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.

incorrect nested model save error

#5807

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!

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

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 15th, 2010 @ 07:15 AM

  2. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:32 AM

  3. Neeraj Singh
    Neeraj Singh

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

    November 10th, 2010 @ 10:19 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 11th, 2011 @ 07:18 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 11th, 2011 @ 07:18 PM