Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

attr_accessible breaks nested model attributes

#4677

If a model with has_many and accepts_nested_attributes_for also has attr_accessible, then you cannot use nested model attributes to create instances of the child model in the parent model.

I confirmed this using a testbed based on the Railscast for Nested Model Forms. Survey accepts_nested_attributes_for :questions. I then run the following code:

Survey.create({:name => "test", :questions_attributes => [ { :content => "a"} ] }).questions.size

If Survey does not have attr_accessible :name, then the result of this statement is 1. If it does have it, then the result is zero.

I've tested this in Rails 2.3.2 and Rails 3, and got the same behavior.

(I originally described this on Stack Overflow)

Craig Walker

Reported by Craig S Walker · May 24th, 2010 @ 12:57 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Kane
    Kane

    RTFM ;)

    accepts_nested_attributes_for(*attr_names)

    Defines an attributes writer for the specified association(s). If you are using attr_protected or attr_accessible, then you will need to add the attribute writer to the allowed list.

    If you dont allow mass-assignment to :questions, :questions will not be assigned.

    please set to invalid.

    June 22nd, 2010 @ 06:47 PM

  2. Andrew White
    Andrew White
    • State changed from new to invalid

    June 25th, 2010 @ 05:35 PM