accepts_nested_attributes_for has_one issue

Hi JB,

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@email.com"},
"commit"=>"Sign up"}
  e[4;36;1mUser Exists (0.0ms)e[0m e[0;1mSELECT "users".id FROM
"users" WHERE ("users"."email" = E'john@email.com') LIMIT 1e[0m
  e[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@email.com',
'2009-05-21 19:25:15.531000', E'pending') RETURNING "id"e[0m
  e[4;36;1mUser Load (0.0ms)e[0m e[0;1mSELECT * FROM "users" WHERE
("users"."id" = 981727452) e[0m
Sent mail to john@email.com

Im still stuck on this, weird!.

JB

I'm having just the same problem at the moment, was there a solution
posted for this?

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

thomas@icdesign.com.au 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.