When creating a new AR::Base object it''s possible to pass a hash of attributes to the constructor. Today I noticed that if there is a value for the primary key attribute in that hash it gets ignored. For instance, r = ORM::Reaction.new(''id'' => 2) => #<ORM::Reaction:0xb792a234 @attributes={"name"=>nil, "spontaneous"=>nil, "note"=>nil, "bidirectional"=>nil, "picture"=>nil, "source"=>nil}, @new_record=true> The attribute ''id'' isn''t there. Is this intentional? Thanks Luca
Dee Zsombor
2005-May-19 07:21 UTC
Re: ActiveRecord::Base#initialize - ignores pk attribute
Rails assumes that primary keys are auto incremented, generated by database itself. So I would say its intentional. Zsombor On 5/18/05, Luca Pireddu <lucap-fVOoFLC7IWo@public.gmane.org> wrote:> When creating a new AR::Base object it''s possible to pass a hash of attributes > to the constructor. Today I noticed that if there is a value for the primary > key attribute in that hash it gets ignored. For instance, > > r = ORM::Reaction.new(''id'' => 2) > => #<ORM::Reaction:0xb792a234 @attributes={"name"=>nil, "spontaneous"=>nil, > "note"=>nil, "bidirectional"=>nil, "picture"=>nil, "source"=>nil}, > @new_record=true> > > The attribute ''id'' isn''t there. Is this intentional? > > Thanks > > Luca > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- http://deezsombor.blogspot.com
Luca Pireddu
2005-May-19 14:28 UTC
Re: ActiveRecord::Base#initialize - ignores pk attribute
On May 19, 2005 01:21, Dee Zsombor wrote:> Rails assumes that primary keys are auto incremented, generated by > database itself. So I would say its intentional. > > ZsomborBut if I wanted the value to be automatically generated, I wouldn''t try to set it :-) Anyways, this can be worked around easily by explicitly assigning a value to ''id'' after the object has been instantiated. Thanks for your feedback. Luca> > On 5/18/05, Luca Pireddu <lucap-fVOoFLC7IWo@public.gmane.org> wrote: > > When creating a new AR::Base object it''s possible to pass a hash ofattributes> > to the constructor. Today I noticed that if there is a value for theprimary> > key attribute in that hash it gets ignored. For instance, > > > > r = ORM::Reaction.new(''id'' => 2) > > => #<ORM::Reaction:0xb792a234 @attributes={"name"=>nil,"spontaneous"=>nil,> > "note"=>nil, "bidirectional"=>nil, "picture"=>nil, "source"=>nil}, > > @new_record=true> > > > > The attribute ''id'' isn''t there. Is this intentional? > > > > Thanks > > > > Luca > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > http://deezsombor.blogspot.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >