When modifying a model that has a has_one or has_many relationship people usually create form that will submit the id of the selected associated object. Rails takes this into consideration with validations: validate_presence_of :country will validate if country or country_id has been set. In Rails, providing the associated ID works fine with has_one but if you try to do this with a has_many: form_for(@country) do |form| form.select :state_ids, @states form.select :state_ids, @states #etc... The selected state_ids are written to the DB as soon as they''re assigned. What''s the reasoning behind this? How is one supposed to assign multiple collection ids via a form? accepts_nested_attributes_for doesn''t work out too well either because you''re just setting IDs -a field that is explicitly ignored by accepts_nested except when testing whether or not the given params are new.
Leonardo Mateo
2009-Nov-14 10:42 UTC
Re: Has Many Relationship: collection_ids= Method Useless?
On Sat, Nov 14, 2009 at 6:37 AM, MaggotChild <hsomob1999-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > When modifying a model that has a has_one or has_many relationship > people usually create form that will submit the id of the selected > associated object. Rails takes this into consideration with > validations: validate_presence_of :country will validate if country > or country_id has been set. > > In Rails, providing the associated ID works fine with has_one but if > you try to do this with a has_many: > > form_for(@country) do |form| > form.select :state_ids, @states > form.select :state_ids, @states > #etc... > > The selected state_ids are written to the DB as soon as they''re > assigned. What''s the reasoning behind this? > How is one supposed to assign multiple collection ids via a form?You have to send them as an array, and assign them as an array too. For example, View form.select("state_ids[]", @states) form.select("state_ids[]", @states) Controller @whatever.state_ids = params[:state_ids] You''ll have to figure out the exact syntax, but that''s the idea. Hope it helps. -- Leonardo Mateo. There''s no place like ~
MaggotChild
2009-Nov-14 17:58 UTC
Re: Has Many Relationship: collection_ids= Method Useless?
On Nov 14, 2:42 am, Leonardo Mateo <leonardoma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sat, Nov 14, 2009 at 6:37 AM, MaggotChild <hsomob1...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > In Rails, providing the associated ID works fine with has_one but if > > you try to do this with a has_many: > > > form_for(@country) do |form| > > form.select :state_ids, @states > > form.select :state_ids, @states > > #etc... > > > The selected state_ids are written to the DB as soon as they''re > > assigned. What''s the reasoning behind this? > > How is one supposed to assign multiple collection ids via a form? > > You have to send them as an array, and assign them as an array too. > > For example, > View > form.select("state_ids[]", @states) > form.select("state_ids[]", @states) > > Controller > @whatever.state_ids = params[:state_ids]That''s the problem. As soon as you make that assignment the IDs are written to the db. You''d have to say: Whatever.tranaction do @what = Whatever.new(params[:whatever]) #other stuff if @what.valid? .... end Which can result in an unnecessarily large transaction scope. Though this is somewhat expected since << and co. also write to the DB on assignment.