Questions tagged [nested-attributes]

RubyOnRails allows you to access attributes of objects associated with the main model, using one, deep hash. Use this tag if you have problem with a model in which you defined `accepts_nested_attributes_for` or if you use nested calls of `fields_for` in a view.

In RubyOnRails you can associate models using has_many, belongs_to or has_one. If you additionally use a macro accepts_nested_attributes_for you can edit these associated objects using a deep hash of attributes assigned to (or read from) the top-level object.

See example:

class User
  belongs_to :company
  accepts_nested_attributes_for :company
end

@user.attributes = {
  :name => "John User",
  :company_attributes => {
    :name => "ACME Ltd.",
  }
}

You can read more in:

1642 questions
161
votes
4 answers

Rails - Strong Parameters - Nested Objects

I've got a pretty simple question. But haven't found a solution so far. So here's the JSON string I send to the server: { "name" : "abc", "groundtruth" : { "type" : "Point", "coordinates" : [ 2.4, 6 ] } } Using the new permit method,…
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
69
votes
2 answers

RoR nested attributes produces duplicates when edit

I'm trying to follow Ryan Bates RailsCast #196: Nested model form part 1. There're two apparent differences to Ryans version: 1) I'm using built-in scaffolding and not nifty as he's using, and 2) I'm running rails 4 (I don't really know what version…
conciliator
  • 6,078
  • 6
  • 41
  • 66
66
votes
8 answers

Rails after_initialize only on "new"

I have the following 2 models class Sport < ActiveRecord::Base has_many :charts, order: "sortWeight ASC" has_one :product, :as => :productable accepts_nested_attributes_for :product, :allow_destroy => true end class Product <…
Tyler DeWitt
  • 23,366
  • 38
  • 119
  • 196
61
votes
4 answers

Does accepts_nested_attributes_for work with belongs_to?

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…
Nick M
  • 939
  • 1
  • 8
  • 9
59
votes
4 answers

accepts_nested_attributes_for with belongs_to polymorphic

I would like set up a polymorphic relation with accepts_nested_attributes_for. Here is the code: class Contact :client end class Job :true …
dombesz
  • 7,890
  • 5
  • 38
  • 47
45
votes
2 answers

Nested models and parent validation

I've got two models. - Parent has_many Children; - Parent accepts_nested_attributes_for Children; class Parent < ActiveRecord::Base has_many :children, :dependent => :destroy accepts_nested_attributes_for :children, :allow_destroy => true …
fl00r
  • 82,987
  • 33
  • 217
  • 237
34
votes
1 answer

Rails 3: How does "accepts_nested_attributes_for" work?

Consider the following association: class Product < ActiveRecord::Base belongs_to :shop accepts_nested_attributes_for :shop end If params[:product][:shop_attributes] = {"name" => "My Shop"} and I do: @product =…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
29
votes
4 answers

Nested attribute update_attributes uses insert rather than update

I have a user and nested profile class as follows: class User < ActiveRecord::Base has_one :profile attr_accessible :profile_attributes accepts_nested_attributes_for :profile end class Profile < ActiveRecord::Base belongs_to :user …
Jason
  • 555
  • 1
  • 4
  • 8
29
votes
4 answers

Can't mass-assign protected attributes

Updating the code formatting for better viewing. Folks, I have been looking at this for sometime but I don't understand what could be messing up here. I am using Devise. class User < ActiveRecord::Base has_many :addresses …
Sanjay
  • 388
  • 1
  • 3
  • 10
29
votes
7 answers

accepts_nested_attributes_for with has_many => :through Options

I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model. Associations: class Link < ActiveRecord::Base has_many :tags, :through => :link_tags has_many :link_tags …
Andrew C
  • 431
  • 1
  • 5
  • 8
29
votes
4 answers

Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship

I cannot seem to get a nested form to generate in a rails view for a belongs_to relationship using the new accepts_nested_attributes_for facility of Rails 2.3. I did check out many of the resources available and it looks like my code should be…
Billy Gray
  • 1,747
  • 4
  • 18
  • 23
27
votes
3 answers

rails ActiveAdmin nested form has_one accepts_attributes_for formtastic issue

I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can…
23
votes
2 answers

Rails nested has_one: cannot delete existing record

I'm trying to update nested question_output attributes in a 'question' model. A question has_one question_output. If there are no existing question_outputs in the database, everything works fine. But if the record already has a question_output, I…
PlankTon
  • 12,443
  • 16
  • 84
  • 153
23
votes
3 answers

How to maintain the ordering for nested attributes when using accepts_nested_attributes_for in a Rails application

Here is the parent model: class TypeWell < ActiveRecord::Base ... has_many :type_well_phases, :dependent => :destroy accepts_nested_attributes_for :type_well_phases, :reject_if => lambda { |a| a[:phase_id].blank? }, :allow_destroy => true …
Bharat
  • 2,409
  • 6
  • 32
  • 57
22
votes
2 answers

Rails nested attributes children callbacks aren't fired

I'm running into a bizarre issue where children callbacks aren't fired when the parent is updated... I have the following model setup: class Budget < ActiveRecord::Base has_many :line_items accepts_nested_attributes_for :line_items end   class…
sethvargo
  • 26,739
  • 10
  • 86
  • 156
1
2 3
99 100