Pete Campbell
2010-12-10 18:50:30 UTC
I have a legacy table with three fields: NAME, VALUE, ID. Naturally, the
primary key is NAME and not ID. The problem is that ActiveRecord updates
the NAME primary key when I try to set ID: object.id=1 will update the
NAME attribute and not the ID attribute. I can only update the ID
attribute via WRITE_ATTRIBUTE (see IRB example below).
I can work around this issue, but it breaks FactoryGirl (and presumably
others). Any thoughts on workarounds or if I'm doing something wrong?
Thanks!
Pete
------
class Sequence < ActiveRecord::Base
set_primary_key "name"
...
TiBook $ irb
>> a = Sequence.first
=> #<Sequence name: "default", value: 1001, id: 1>
>> a.id = 2
=> 2
>> a
=> #<Sequence name: 2, value: 1001, id: 1>
>> a.send('id=', 3)
=> 3
>> a
=> #<Sequence name: 3, value: 1001, id: 1>
>> a.write_attribute('id', 3)=> 3
>> a
=> #<Sequence name: 3, value: 1001, id: 3>
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
primary key is NAME and not ID. The problem is that ActiveRecord updates
the NAME primary key when I try to set ID: object.id=1 will update the
NAME attribute and not the ID attribute. I can only update the ID
attribute via WRITE_ATTRIBUTE (see IRB example below).
I can work around this issue, but it breaks FactoryGirl (and presumably
others). Any thoughts on workarounds or if I'm doing something wrong?
Thanks!
Pete
------
class Sequence < ActiveRecord::Base
set_primary_key "name"
...
TiBook $ irb
>> a = Sequence.first
=> #<Sequence name: "default", value: 1001, id: 1>
>> a.id = 2
=> 2
>> a
=> #<Sequence name: 2, value: 1001, id: 1>
>> a.send('id=', 3)
=> 3
>> a
=> #<Sequence name: 3, value: 1001, id: 1>
>> a.write_attribute('id', 3)=> 3
>> a
=> #<Sequence name: 3, value: 1001, id: 3>
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.