I''m trying to (cleanly) update a habtm Model after a form submission. Along with everything else in the form (that relates to the Account Model) I have a series of checkboxes: <%= check_box_tag "account[roles][]", role.id, @account.roles.include?(role) %> That all works right, so upon submission, my AccountController does this: def update @account = Account.find(@params[:id]) @account.roles.clear @account.roles << Role.find(@params[:roles]) if @params[:roles] if @account.update_attributes(@params[:account]) ... end end That also works fine... the problem is, I don''t want to have to do all of that manually (I have a ton of habtm''s). Isn''t there an easy way to make that habtm association update the related tables automagically? I tried overwriting the update_attributes method in my Accounts Model: def update_attributes(params) roles.clear roles << Role.find(params[:roles]) if params[:roles] super end Which kind of works, but there''s an exception raised when ActiveRecord::Base tries to store params[:roles] (which typically looks like ["1", "3", "4"]) as Roles in the related table. I''d love to hear some tips, thanks, Mike
Mike Evans <mike-FpmKrjSK9lFgyGvZe3TC8g@public.gmane.org> writes:> def update_attributes(params) > roles.clear > roles << Role.find(params[:roles]) if params[:roles] > super > end > > Which kind of works, but there''s an exception raised when > ActiveRecord::Base tries to store params[:roles] (which typically looks > like ["1", "3", "4"]) as Roles in the related table.Search the archives for "habtm and checkboxes". We''ve just finished up a nice thread this week that covers this quite well. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Michael Campbell
2005-May-21 14:19 UTC
Symbols vs strings as @param hash keys, was "updating habtm from a form"
On 5/20/05, Mike Evans <mike-FpmKrjSK9lFgyGvZe3TC8g@public.gmane.org> wrote:> I''m trying to (cleanly) update a habtm Model after a form submission. > > Along with everything else in the form (that relates to the Account > Model) I have a series of checkboxes: > <%= check_box_tag "account[roles][]", role.id, > @account.roles.include?(role) %> > > That all works right, so upon submission, my AccountController does this: > def update > @account = Account.find(@params[:id]) > @account.roles.clear > @account.roles << Role.find(@params[:roles]) if @params[:roles] > if @account.update_attributes(@params[:account])I notice Mike is using symbols as hash keys there; the examples I''ve seen use strings ''id'', ''roles'', and so on, so that''s what I''ve been using. Is one more "correct" than the other, or more standard, or does it matter. Can there be a case where one works and the other doesn''t?
Michael Schuerig
2005-Jun-03 16:31 UTC
Re: Symbols vs strings as @param hash keys, was "updating habtm from a form"
On Saturday 21 May 2005 16:19, Michael Campbell wrote:> I notice Mike is using symbols as hash keys there; the examples I''ve > seen use strings ''id'', ''roles'', and so on, so that''s what I''ve been > using. Is one more "correct" than the other, or more standard, or > does it matter. Can there be a case where one works and the other > doesn''t?I''m wondering the same all the time. Rails internally extends Hash (through HashWithIndifferentAccess) to be agnostic regarding the string/symbol distinction. That doesn''t help me much, though, when trying to decide how to write my own code. Michael -- Michael Schuerig Not only does lightning not strike mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org twice, it usually doesn''t strike once. http://www.schuerig.de/michael/ --Salman Rushdie, Fury
Michael Campbell
2005-Jun-03 18:23 UTC
Re: Re: Symbols vs strings as @param hash keys, was "updating habtm from a form"
On 6/3/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> On Saturday 21 May 2005 16:19, Michael Campbell wrote: > > > I notice Mike is using symbols as hash keys there; the examples I''ve > > seen use strings ''id'', ''roles'', and so on, so that''s what I''ve been > > using. Is one more "correct" than the other, or more standard, or > > does it matter. Can there be a case where one works and the other > > doesn''t? > > I''m wondering the same all the time. Rails internally extends Hash > (through HashWithIndifferentAccess) to be agnostic regarding the > string/symbol distinction. That doesn''t help me much, though, when > trying to decide how to write my own code.>From what I gather on IRC, it doesn''t matter. But, to be consistentwith "current wisdom", I''m using symbols now.