On Dec 10, 2007, at 5:38 PM, Sam K. wrote:
If you think I’m doing it wrong, can you suggest a good way?
You can use the existing key field and key value. I’m doing this in
apps being rewritten, as well as in databases that are being shared
by Rails & other platforms. I have found no need so far to insist on
the autoinc field.
In your model, if the field name is not “id,” you can declare the
keyfield like this:
self.primary_key = “rcrd_id” # or whatever the name is
When you declare associations, you can use the :foreign_key => ‘xxx’
option in :has_one,:has_many etc to define a field name that does not
follow Rails’ convention.
The only tricky part is when *reading the key value, you can specify
the field as my_model.rcrd_id (as expected), but when *writing the
value, you still have to use the Rails default of my_model.id
(presumably because .id is a manually written accessor method), so
the name doesn’t change even when the field name does.
Next, add a method to each ActiveRecord model similar to this one to
create the key value for when new records are created:
self.id = 20.random_id
That .random_id is custom extension I use, so do whatever it is you
normally do to generate your keyfield values.
Is that the kind of info you’re looking for?