Iβm not quite sure what my problem is, therefore this question may require a more detailed explanation, but here is what seems most relevant:
I have has_many :through , and in the join model there are several fields that are not foreign keys. When I create models and try to save, I get a validation error for non-external key fields from the join model.
My files look like this:
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 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
the returned parameters are as follows:
{"first_name"=>"", "last_name"=>"", "weddings_attributes"=>{"0"=>{"wedding_assignments_attributes"=>{"0"=>{"role"=>"Bride Maid", "budget"=>""}}, "date"=>"", "ceremony_notes"=>""}}}
And the exact error:
ActiveRecord::RecordInvalid in PeopleController
This is clearly wrong, as you can see it in params[]
What am I doing wrong?
This is rails 3.0.0
ruby-on-rails ruby-on-rails-3 has-many-through
Sooodesune
source share