Accepts_nested_attributes_for has_one issue

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

[email protected]_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://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

Hi JB,

On May 21, 4:24 pm, John B. [email protected]
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’m having just the same problem at the moment, was there a solution
posted for this?

On May 22, 5:45 am, John B. [email protected]

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”=>“[email protected]”},
“commit”=>“Sign up”}
e[4;36;1mUser Exists (0.0ms)e[0m e[0;1mSELECT “users”.id FROM
“users” WHERE (“users”.“email” = E’[email protected]’) 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’[email protected]’,
‘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 [email protected]

Im still stuck on this, weird!.

JB

On May 22, 5:45�am, John B. [email protected]

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.

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 S. [email protected]

[email protected] 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 B. [email protected]

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

[email protected] 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 S. [email protected]

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.

Richard S. wrote:

[email protected] 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 S. [email protected]

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

You beat me to the punch! :slight_smile:

Richard S. wrote:

[email protected] 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 S. [email protected]

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