This project is archived and is in readonly mode.

#5807 ✓stale
SooDesuNe

incorrect nested model save error

Reported by SooDesuNe | October 14th, 2010 @ 02:32 PM

I'm not exactly sure what the cause of the problem is, so this may require some more clarification, but here's what seems to be most relevant:

I have a has_many :through and the join model has some fields that aren't foreign keys. When I build the models up and try to save I get a validation error on the non-foreign key fields from the join model.

My files look like:

Person.rb

  has_many :wedding_assignments, :dependent => :destroy
  has_many :weddings, :through=>:wedding_assignments
  accepts_nested_attributes_for :weddings
  accepts_nested_attributes_for :wedding_assignments

Wedding.rb

  has_many :wedding_assignments, :dependent => :destroy
  has_many :people, :through=>:wedding_assignments
  accepts_nested_attributes_for :people
  accepts_nested_attributes_for :wedding_assignments

WeddingAssignment.rb

  belongs_to :person
  belongs_to :wedding
  validates_presence_of :role, :person, :wedding

(role is a string)

people_controller.rb

  def new
    @person = Person.new

    1.times do
      wedding = @person.weddings.build
      1.times do
        assignment = wedding.wedding_assignments.build
        assignment.person = @person
        assignment.wedding = wedding
      end
    end
  end

  def create
    @person = Person.new(params[:person])
    @person.weddings.each do |wedding|
      wedding.wedding_assignments.each do |assignment|
        assignment.person = @person  #i don't think I should need to set person and wedding manually, but I get a validation error if I don't
        assignment.wedding = wedding
      end
    end
 end

the params that come back look like:

{"first_name"=>"", "last_name"=>"", "weddings_attributes"=>{"0"=>{"wedding_assignments_attributes"=>{"0"=>{"role"=>"Bride's Maid", "budget"=>""}}, "date"=>"", "ceremony_notes"=>""}}}

And the exact error is:

ActiveRecord::RecordInvalid in PeopleController#create
Validation failed: Role can't be blank

Which is clearly not correct, since you can see it in params[]

The expected behavior of course is for the save to succeed. This can be duplicated in the console. Inspecting the value of person.weddings.first.wedding_assignments.first.role gives "Bride's Maid", so I know it's there.

This is rails 3.0.0 on a mac

I'm not sure what else I can check to debug this. I would really appreciate some advice.

Thank you so much for the beautiful thing that is rails 3!

Comments and changes to this ticket

  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer October 15th, 2010 @ 07:15 AM

    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:32 AM

    Automatic cleanup of spam.

  • Neeraj Singh

    Neeraj Singh November 10th, 2010 @ 10:19 PM

    @SooDesuNe Can you verify if it is still an issue in rails edge. If yes then I will look into it.

  • Santiago Pastorino

    Santiago Pastorino February 11th, 2011 @ 07:18 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 11th, 2011 @ 07:18 PM

    • State changed from “open” to “stale”

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages