tirkal
2005-Oct-16 09:35 UTC
Why is it not possible to set an ActiveRecord instance "id" attribute by providing it as a hash key to the contructor?
Hi I noticed it isn''t possible to set an ActiveRecord instace''s "id" attribute from the contructor, the way it''s possible to set other attributes. e.g.: [MySQL] create table foos (id int primary key, name text); [irb session inside Rails] class Foo < ActiveRecord::Base; end foo = Foo.new(:id => 123, :name => "bar") #=> #<LoginController::Foo:0x40bf0364 @new_record=true, @attributes={"name"=>"bar"}> foo.id #=> nil foo.id = 123 foo.id #=> 123 ---------------------end of code--------------------- Why is that so? It seems to me that the "id" attribute is extremely important, and it would seem logical that it should be possible to set it right from the constructor, like the rest of the attributes. Regards, Tirkal
Robert Jones
2005-Oct-16 10:26 UTC
Re: Why is it not possible to set an ActiveRecord instance "id" attribute by providing it as a hash key to the contructor?
tirkal wrote:> Hi > > I noticed it isn''t possible to set an ActiveRecord instace''s "id" > attribute from the contructor, the way it''s possible to set other > attributes. > > e.g.: > > [MySQL] > create table foos (id int primary key, name text); > > [irb session inside Rails] > > class Foo < ActiveRecord::Base; end > > foo = Foo.new(:id => 123, :name => "bar") > #=> #<LoginController::Foo:0x40bf0364 @new_record=true, > @attributes={"name"=>"bar"}> > > foo.id > #=> nil > > foo.id = 123 > > foo.id > #=> 123 > ---------------------end of code--------------------- > > Why is that so? It seems to me that the "id" attribute is extremely > important, and it would seem logical that it should be possible to set > it right from the constructor, like the rest of the attributes. > > Regards, > TirkalSeems right to me. id is the primary key, auto-incremented - it gets set automatically when you create a record, and never changes thereafter. Why would you want to set the id manually? That way lies a world of pain! If you have some attribute like an id that you want to be able to set manually, then it isn''t really the id. -- Robert Jones
tirkal
2005-Oct-16 18:53 UTC
Re: Re: Why is it not possible to set an ActiveRecord instance "id" attribute by providing it as a hash key to the contructor?
well, in my case: 1. the id isn''t auto-incremet. 2. each item has a specific id. 3. the id is meaningul (informative), and is set at construction time. So it would really be more convenient and natural to be able to set it in the contruction. I would save that additional "foo.id = 123" which is what I do right now. I''d appreciate it if I could do it the more natural way. Regards, Tirkal On 10/16/05, Robert Jones <jonesieboy-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org> wrote:> tirkal wrote: > > > Hi > > > > I noticed it isn''t possible to set an ActiveRecord instace''s "id" > > attribute from the contructor, the way it''s possible to set other > > attributes. > > > > e.g.: > > > > [MySQL] > > create table foos (id int primary key, name text); > > > > [irb session inside Rails] > > > > class Foo < ActiveRecord::Base; end > > > > foo = Foo.new(:id => 123, :name => "bar") > > #=> #<LoginController::Foo:0x40bf0364 @new_record=true, > > @attributes={"name"=>"bar"}> > > > > foo.id > > #=> nil > > > > foo.id = 123 > > > > foo.id > > #=> 123 > > ---------------------end of code--------------------- > > > > Why is that so? It seems to me that the "id" attribute is extremely > > important, and it would seem logical that it should be possible to set > > it right from the constructor, like the rest of the attributes. > > > > Regards, > > Tirkal > > Seems right to me. id is the primary key, auto-incremented - it gets set > automatically when you create a record, and never changes thereafter. Why > would you want to set the id manually? That way lies a world of pain! If > you have some attribute like an id that you want to be able to set > manually, then it isn''t really the id. > > -- > Robert Jones > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Robert Jones
2005-Oct-16 20:22 UTC
Re: Re: Why is it not possible to set an ActiveRecord instance "id" attribute by providing it as a hash key to the contructor?
tirkal wrote:> well, in my case: > > 1. the id isn''t auto-incremet. > 2. each item has a specific id. > 3. the id is meaningul (informative), and is set at construction time. > > So it would really be more convenient and natural to be able to set it > in the contruction. I would save that additional "foo.id = 123" which > is what I do right now. I''d appreciate it if I could do it the more > natural way. > > Regards, > Tirkal > > On 10/16/05, Robert Jones > <jonesieboy-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org> wrote: >> tirkal wrote: >> >> > Hi >> > >> > I noticed it isn''t possible to set an ActiveRecord instace''s "id" >> > attribute from the contructor, the way it''s possible to set other >> > attributes. >> > >> > e.g.: >> > >> > [MySQL] >> > create table foos (id int primary key, name text); >> > >> > [irb session inside Rails] >> > >> > class Foo < ActiveRecord::Base; end >> > >> > foo = Foo.new(:id => 123, :name => "bar") >> > #=> #<LoginController::Foo:0x40bf0364 @new_record=true, >> > @attributes={"name"=>"bar"}> >> > >> > foo.id >> > #=> nil >> > >> > foo.id = 123 >> > >> > foo.id >> > #=> 123 >> > ---------------------end of code--------------------- >> > >> > Why is that so? It seems to me that the "id" attribute is extremely >> > important, and it would seem logical that it should be possible to set >> > it right from the constructor, like the rest of the attributes. >> > >> > Regards, >> > Tirkal >> >> Seems right to me. id is the primary key, auto-incremented - it gets set >> automatically when you create a record, and never changes thereafter. >> Why >> would you want to set the id manually? That way lies a world of pain! >> If you have some attribute like an id that you want to be able to set >> manually, then it isn''t really the id. >> >> -- >> Robert Jones >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >>OK - I''m not looking to get into a flame war here - you do it however you want, BUT.... 1. The id should be auto-increment :) 2/3. If each item has a specific id, presumably from some external representation, then the id you are referring to should be stored as a separate attribute. By all means store that in your Rails app, but don''t use it as your primary key. To paraphrase the Rails book, using some external id as your primary key is a bad idea because external ids are potentially subject to change. What do you do if the external ids all need to have "003" prepended to them for some reason, for example? You''ll have to do cascading updates on all references to the ids as foreign keys in other tables. Big hassle. If you leave the external id as an attribute, then changes to the external ids present no problem. In the words of the Rails book, you''ve "decoupled the knitting together of rows from the external representation of data in those rows". Hope this is helpful. -- Robert Jones
zdennis
2005-Oct-16 23:10 UTC
Re: Re: Re: Why is it not possible to set an ActiveRecord instance "id" attribute by providing it as a hash key to the contructor?
Robert Jones wrote:> OK - I''m not looking to get into a flame war here - you do it however you > want, BUT.... > > 1. The id should be auto-increment :) > > 2/3. If each item has a specific id, presumably from some external > representation, then the id you are referring to should be stored as a > separate attribute. By all means store that in your Rails app, but don''t > use it as your primary key.I completely agree with this. You will save yourself a world of headache if you do things this way. Never use an attribute of the data which is meaningful to the information you are storing as the primary key. Let the primary key only have meaning inside to the database that uses it. This will avoid so many future problems that may occur. Just my 2 cents. Zach