Forum: Ruby on Rails accepts_nested_attributes_for has_one issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (johnnybutler7)
on 2009-05-21 17:24
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-...
http://stackoverflow.com/questions/742536/rails-ne...
http://guides.rubyonrails.org/2_3_release_notes.ht...
http://ryandaigle.com/articles/2009/2/1/what-s-new...

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
548dde5574b950a8ac2f09cb0ee681d5?d=identicon&s=25 Andrew France (Guest)
on 2009-05-21 20:34
(Received via mailing list)
Hi JB,

On May 21, 4:24 pm, John Butler <rails-mailing-l...@andreas-s.net>
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
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (johnnybutler7)
on 2009-05-21 21:45
>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"}
  User Exists (0.0ms)   SELECT "users".id FROM
"users" WHERE ("users"."email" = E'john@email.com') LIMIT 1
  INSERT 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"
  User Load (0.0ms)   SELECT * FROM "users" WHERE
("users"."id" = 981727452) 
Sent mail to john@email.com

Im still stuck on this, weird!.

JB
875eb7328850c079c8f9ec32d19f6b45?d=identicon&s=25 thomas@icdesign.com.au (Guest)
on 2009-06-04 09:02
(Received via mailing list)
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...@andreas-s.net>
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (johnnybutler7)
on 2009-06-04 10:43
thomas@icdesign.com.au 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...@andreas-s.net>

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
391f0c678ce2f62b7cbfa33cb0b2f235?d=identicon&s=25 Richard Shank (iampersistent)
on 2009-06-10 08:05
>> On May 22, 5:45�am, John Butler <rails-mailing-l...@andreas-s.net>
>
> 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.
875eb7328850c079c8f9ec32d19f6b45?d=identicon&s=25 thomas@icdesign.com.au (Guest)
on 2009-06-10 08:19
(Received via mailing list)
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>
391f0c678ce2f62b7cbfa33cb0b2f235?d=identicon&s=25 Richard Shank (iampersistent)
on 2009-06-10 12:44
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.
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (johnnybutler7)
on 2009-06-10 12:53
Richard Shank wrote:
> 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.

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
391f0c678ce2f62b7cbfa33cb0b2f235?d=identicon&s=25 Richard Shank (iampersistent)
on 2009-06-10 12:54
Richard Shank wrote:
> 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.

The solution is to add :childmodelname_attributes to the parent
attr_accessible
391f0c678ce2f62b7cbfa33cb0b2f235?d=identicon&s=25 Richard Shank (iampersistent)
on 2009-06-10 13:05
You beat me to the punch! :)
This topic is locked and can not be replied to.