I'm not exactly sure what my problem is, so this question 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[]
What am I doing wrong?
This is rails 3.0.0