Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship

I’m a few months too late, but I was looking to solve this error and my situation was that I could not change the relationship to ‘face the other way’.

The answer really is quite simple, you have to do this in your new action:

@account.build_owner

The reason why the form did not display using fields_for was because it did not have a valid object. You had the right idea up there with:

@account.owner.build

However, this is not the way belongs_to work. This method is only generated with has_many and has_and_belongs_to_many.

Reference:
http://guides.rubyonrails.org/association_basics.html#belongs-to-association-reference

Leave a Comment