61

I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship?

class User < ActiveRecord::Base
  belongs_to :organization
  accepts_nested_attributes_for :organization
end

class Organization < ActiveRecord::Base
  has_many :users
end

In a view:

= form_for @user do |f|
  f.label :name, "Name"
  f.input :name

  = f.fields_for :organization do |o|
    o.label :city, "City"
    o.input :city

  f.submit "Submit"
Nick M
  • 939
  • 1
  • 8
  • 9
  • 1
    The docs http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html don't mention `belongs_to` so I doubt it. Why don't you try it and get back to us? – epochwolf Nov 02 '11 at 04:38
  • I was able to verify that at least as of Rails 5.2 the answer is yes, it works. [This answer](https://stackoverflow.com/a/51968475/199712) helped me. – Jason Swett Nov 02 '18 at 20:33

4 Answers4

26

Nested attributes appear to work fine for a belongs_to association as of Rails 4. It might have been changed in an earlier version of Rails, but I tested in 4.0.4 and it definitely works as expected.

maletor
  • 7,072
  • 7
  • 42
  • 63
kid_drew
  • 3,857
  • 6
  • 28
  • 38
  • 13
    Still in Rails 4.1.1, accepts_nested_attributes doesn't work with *polymorphic* belongs_to. I had to move it to another (has_one) side of the association. This is just to share the info with others. – Tatiana Tyu Jun 25 '14 at 12:56
  • I agree with kid_drew. I just got it to work in Rails version 4.2.9. – Tintin81 Oct 20 '17 at 12:12
21

The doc epochwolf cited states in the first line "Nested attributes allow you to save attributes on associated records through the parent." (my emphasis).

You might be interested in this other SO question which is along the same lines as this one. It describes two possible solutions: 1) moving the accepts_nested_attributes to the other side of the relationship (in this case, Organization), or 2) using the build method to build the Organization in the User before rendering the form.

I also found a gist that describes a potential solution for using accepts_nested_attributes with a belongs_to relationship if you're willing to deal with a little extra code. This uses the build method as well.

Community
  • 1
  • 1
robmclarty
  • 2,215
  • 2
  • 20
  • 21
  • I've updated that gist for Rails 4: https://gist.github.com/dmzza/ce9b6e660c576039afca984cda0f0aed – dmzza Mar 29 '16 at 22:49
  • For those looking for an answer showing exactly how `accepts_nested_attributes_for` can work with `belongs_to`, I found [this answer](https://stackoverflow.com/a/51968475/199712) to be the most helpful. – Jason Swett Nov 02 '18 at 20:36
11

For belongs_to association in Rails 3.2, nested model needs the following two steps:

(1) Add new attr_accessible to your child-model (User model).

accepts_nested_attributes_for :organization
attr_accessible :organization_attributes

(2) Add @user.build_organization to your child-controller (User controller) in order to create column organization.

def new
  @user = User.new
  @user.build_organization
end
user3551164
  • 111
  • 1
  • 4
  • I'm doing this, but I am running into a lot of problems getting it to work. having the parent accept nested parameters for its child doesn't seem to be enough for it to build properly, it expects – Csteele5 Jan 28 '16 at 02:38
7

For Ruby on Rails 5.2.1

class User < ActiveRecord::Base
  belongs_to :organization
  accepts_nested_attributes_for :organization
end

class Organization < ActiveRecord::Base
  has_many :users
end

Just got to your controller, suppose to be "users_controller.rb":

Class UsersController < ApplicationController

    def new
        @user = User.new
        @user.build_organization
    end
end

And the view just as Nick did:

= form_for @user do |f|
  f.label :name, "Name"
  f.input :name

  = f.fields_for :organization do |o|
    o.label :city, "City"
    o.input :city

  f.submit "Submit"

At end we see that @user3551164 have already solved, but now (Ruby on Rails 5.2.1) we don't need the attr_accessible :organization_attributes

Fábio Araújo
  • 495
  • 8
  • 11
  • 2
    I also needed to modify my strong params to make this work. In the case of the above example I think I would need to add something like `organization_attributes: [:city]` to my strong params. – Jason Swett Nov 02 '18 at 20:32