This project is archived and is in readonly mode.

#4677 ✓ invalid
Craig S Walker

attr_accessible breaks nested model attributes

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

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

Comments and changes to this ticket

  • Kane

    Kane June 22nd, 2010 @ 06:47 PM

    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.

  • Andrew White

    Andrew White June 25th, 2010 @ 05:35 PM

    • State changed from “new” to “invalid”
  • xiaobao
  • jim123456

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Tickets have moved to Github

The new ticket tracker is available at https://github.com/rails/rails/issues

Shared Ticket Bins

Pages