Hi, Im having an issue with accepts_nested_attributes_for for a has_one relationship. I have posted the code below and the parameters sent to the create but this is not saving the user_detail association record. Ive tried various solutions from the links below but cant get this to work. Can anyone suggest what im doing wrong? class User < ActiveRecord::Base has_one :user_detail, :dependent => :destroy accepts_nested_attributes_for :user_detail attr_accessible :first_name, :user_detail_attributes end def new @user = User.new #@user.build_user_detail end - form_for([:members, @user]) do |user_form| = user_form.label ''first_name'', ''First Name'' = user_form.text_field :first_name -@user.build_user_detail unless @user.user_detail -user_form.fields_for :user_detail do |ud_form| %li = ud_form.label ''website_url'' = ud_form.text_field :website_url "user"=>{"user_detail_attributes"=>{"website_url"=>"www.bbc.co.uk"}, "commit"=>"Sign up"} http://www.pixellatedvisions.com/2009/03/18/rails-2-3-nested-model-forms-and-nil-new-record http://stackoverflow.com/questions/742536/rails-nested-object-form-attributes-problem http://guides.rubyonrails.org/2_3_release_notes.html#nested-object-forms http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes I also tried replacing -user_form.fields_for :user_detail do |ud_form| with -user_form.fields_for @user.user_detail do |ud_form| but then i got UserDetail(#48943070) expected, got HashWithIndifferentAccess(#23663900) JB -- Posted via http://www.ruby-forum.com/.
Hi JB, On May 21, 4:24 pm, John Butler <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Im having an issue with accepts_nested_attributes_for for a has_one > relationship. I have posted the code below and the parameters sent to > the create but this is not saving the user_detail association record. >View code looks fine at a glance especially as it''s producing the right params below.> "user"=>{"user_detail_attributes"=>{"website_url"=>"www.bbc.co.uk"}, > "commit"=>"Sign up"}I would try doing the equivalent on the Rails console, so just loading a user object, setting ''user_detail_attributes'' attribute, then saving and see if that works so any view/controller code can be ruled out. Also I think posting the code in the controller''s create action would be useful. Could possibly be an interaction with attr_accessible so I would temporarily disable that to see if it makes a difference. Hope that helps, Andrew
>I would try doing the equivalent on the Rails console, so just loading > a user object, setting ''user_detail_attributes'' attribute, then saving > and see if that works so any view/controller code can be ruled out. > Also I think posting the code in the controller''s create action would > be useful. Could possibly be an interaction with attr_accessible so I > would temporarily disable that to see if it makes a difference. >Hi Andrew, Doesnt work when trying to create via the rails_console, it does work ok when i do an edit on an exisiting model(details below). user = User.find(:first) user.user_detail_attributes = {"website_url" => "www.bbc.co.uk"} => {"website_url" => "www.bbc.co.uk"} user.save user.user_details => expected_object_details...... I commented out attr_accessible and went through the create code again but still got no user_details saving. Processing Members::UsersController#create (for 127.0.0.1 at 2009-05-21 20:25:15) [POST] Parameters: {"user"=>{"user_detail_attributes"=>{"website_url"=>"www.bbc.co.uk"}, "password_confirmation"=>"xxxxx", "terms"=>"1", "first_name"=>"John", "password"=>"xxxxxx", "last_name"=>"Butler", "email"=>"john-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org"}, "commit"=>"Sign up"} [4;36;1mUser Exists (0.0ms) [0m [0;1mSELECT "users".id FROM "users" WHERE ("users"."email" = E''john-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org'') LIMIT 1 [0m [0mINSERT INTO "users" ("salt", "updated_at", "activated_at", "crypted_password", "vat_number", "title", "user_status_id", "creator_id", "deleted_at", "remember_token_expires_at", "activation_code", "role_id", "preferred_currency_id", "password_reset_code", "reseller_user_id", "updater_id", "reseller_logo", "remember_token", "terms", "first_name", "reseller_code", "last_name", "email", "created_at", "state") VALUES(E''968befce88854acfe386fca4b80aea292b0c5470'', ''2009-05-21 19:25:15.531000'', NULL, E''dd5be536373ce67d3e60b5acc3d2f811330a4062'', NULL, NULL, 2, NULL, NULL, NULL, E''425574bd1b0d7d81f997f0a8f9b294d23386aee5'', 2, NULL, NULL, NULL, NULL, NULL, NULL, ''t'', E''John'', NULL, E''Butler'', E''john-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org'', ''2009-05-21 19:25:15.531000'', E''pending'') RETURNING "id" [0m [4;36;1mUser Load (0.0ms) [0m [0;1mSELECT * FROM "users" WHERE ("users"."id" = 981727452) [0m Sent mail to john-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org Im still stuck on this, weird!. JB -- Posted via http://www.ruby-forum.com/.
thomas-Eg9ij0XiW6Y0n/F98K4Iww@public.gmane.org
2009-Jun-04 07:01 UTC
Re: accepts_nested_attributes_for has_one issue
I''m having just the same problem at the moment, was there a solution posted for this? On May 22, 5:45 am, John Butler <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> >I would try doing the equivalent on the Rails console, so just loading > > a user object, setting ''user_detail_attributes'' attribute, then saving > > and see if that works so any view/controller code can be ruled out. > > Also I think posting the code in the controller''s create action would > > be useful. Could possibly be an interaction with attr_accessible so I > > would temporarily disable that to see if it makes a difference. > > Hi Andrew, > > Doesnt work when trying to create via the rails_console, it does work ok > when i do an edit on an exisiting model(details below). > > user = User.find(:first) > user.user_detail_attributes = {"website_url" => "www.bbc.co.uk"} > => {"website_url" => "www.bbc.co.uk"} > user.save > user.user_details > => expected_object_details...... > > I commented out attr_accessible and went through the create code again > but still got no user_details saving. > > Processing Members::UsersController#create (for 127.0.0.1 at 2009-05-21 > 20:25:15) [POST] > Parameters: > {"user"=>{"user_detail_attributes"=>{"website_url"=>"www.bbc.co.uk"}, > "password_confirmation"=>"xxxxx", "terms"=>"1", "first_name"=>"John", > "password"=>"xxxxxx", "last_name"=>"Butler", "email"=>"j...@email.com"}, > "commit"=>"Sign up"} > [4;36;1mUser Exists (0.0ms) [0m [0;1mSELECT "users".id FROM > "users" WHERE ("users"."email" = E''j...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org'') LIMIT 1 [0m > [0mINSERT INTO "users" ("salt", "updated_at", "activated_at", > "crypted_password", "vat_number", "title", "user_status_id", > "creator_id", "deleted_at", "remember_token_expires_at", > "activation_code", "role_id", "preferred_currency_id", > "password_reset_code", "reseller_user_id", "updater_id", > "reseller_logo", "remember_token", "terms", "first_name", > "reseller_code", "last_name", "email", "created_at", "state") > VALUES(E''968befce88854acfe386fca4b80aea292b0c5470'', ''2009-05-21 > 19:25:15.531000'', NULL, E''dd5be536373ce67d3e60b5acc3d2f811330a4062'', > NULL, NULL, 2, NULL, NULL, NULL, > E''425574bd1b0d7d81f997f0a8f9b294d23386aee5'', 2, NULL, NULL, NULL, NULL, > NULL, NULL, ''t'', E''John'', NULL, E''Butler'', E''j...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org'', > ''2009-05-21 19:25:15.531000'', E''pending'') RETURNING "id" [0m > [4;36;1mUser Load (0.0ms) [0m [0;1mSELECT * FROM "users" WHERE > ("users"."id" = 981727452) [0m > Sent mail to j...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org > > Im still stuck on this, weird!. > > JB > > -- > Posted viahttp://www.ruby-forum.com/.
thomas-Eg9ij0XiW6Y0n/F98K4Iww@public.gmane.org wrote:> I''m having just the same problem at the moment, was there a solution > posted for this? > > On May 22, 5:45�am, John Butler <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>No i didnt find a solution but ill have to revisit it in the next couple of days, looks like ti should work but ill have to get into the guts of whats happening to find out, JB -- Posted via http://www.ruby-forum.com/.
>> On May 22, 5:45�am, John Butler <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt> > > No i didnt find a solution but ill have to revisit it in the next couple > of days, > > looks like ti should work but ill have to get into the guts of whats > happening to find out, > > JBAny progress on this? I''m in the middle of the same problem. -- Posted via http://www.ruby-forum.com/.
thomas-Eg9ij0XiW6Y0n/F98K4Iww@public.gmane.org
2009-Jun-10 06:17 UTC
Re: accepts_nested_attributes_for has_one issue
Richard, I had a go at it the other day and strangely enough it was all working fine, I threw my demo up on github so maybe you can check it out to help fix your problem: http://github.com/anathematic/has_one_problem/tree/master Thomas On Jun 10, 4:05 pm, Richard Shank <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> >> On May 22, 5:45 am, John Butler <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > No i didnt find a solution but ill have to revisit it in the next couple > > of days, > > > looks like ti should work but ill have to get into the guts of whats > > happening to find out, > > > JB > > Any progress on this? I''m in the middle of the same problem. > -- > Posted viahttp://www.ruby-forum.com/.
thomas-Eg9ij0XiW6Y0n/F98K4Iww@public.gmane.org wrote:> Richard, > I had a go at it the other day and strangely enough it was all working > fine, I threw my demo up on github so maybe you can check it out to > help fix your problem: > > http://github.com/anathematic/has_one_problem/tree/master > > Thomas > > > > On Jun 10, 4:05�pm, Richard Shank <rails-mailing-l...@andreas-s.net>Actually, my problem had to do with having attr_accessable in the parent model. My clue was getting a message in the console "WARNING: Can''t mass-assign these protected attributes:" Thanks for the post, I did use your code to start rebuilding to find out where my problem was. -- Posted via http://www.ruby-forum.com/.
Richard Shank wrote:> thomas-Eg9ij0XiW6Y0n/F98K4Iww@public.gmane.org wrote: >> Richard, >> I had a go at it the other day and strangely enough it was all working >> fine, I threw my demo up on github so maybe you can check it out to >> help fix your problem: >> >> http://github.com/anathematic/has_one_problem/tree/master >> >> Thomas >> >> >> >> On Jun 10, 4:05�pm, Richard Shank <rails-mailing-l...@andreas-s.net> > > Actually, my problem had to do with having attr_accessable in the parent > model. My clue was getting a message in the console "WARNING: Can''t > mass-assign these protected attributes:" > > Thanks for the post, I did use your code to start rebuilding to find out > where my problem was.You need to add yuor nested model attributes to your attr_accessible incase you havent already attr_accessible :user_detail_attributes Ill be looking at this failry shortly so let me know if yuo fix it first! JB -- Posted via http://www.ruby-forum.com/.
Richard Shank wrote:> thomas-Eg9ij0XiW6Y0n/F98K4Iww@public.gmane.org wrote: >> Richard, >> I had a go at it the other day and strangely enough it was all working >> fine, I threw my demo up on github so maybe you can check it out to >> help fix your problem: >> >> http://github.com/anathematic/has_one_problem/tree/master >> >> Thomas >> >> >> >> On Jun 10, 4:05�pm, Richard Shank <rails-mailing-l...@andreas-s.net> > > Actually, my problem had to do with having attr_accessable in the parent > model. My clue was getting a message in the console "WARNING: Can''t > mass-assign these protected attributes:" > > Thanks for the post, I did use your code to start rebuilding to find out > where my problem was.The solution is to add :childmodelname_attributes to the parent attr_accessible -- Posted via http://www.ruby-forum.com/.