6

我不确定我的问题是什么,所以这个问题可能需要更多澄清,但这里似乎最相关:

我有一个has_many :through并且连接模型有一些不是外键的字段。当我建立模型并尝试保存时,我在连接模型的非外键字段上收到验证错误。

我的文件看起来像:

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

(角色是一个字符串)

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

返回的参数如下所示:

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

确切的错误是:

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

这显然是不正确的,因为您可以在params[]

我究竟做错了什么?

这是轨道 3.0.0

4

2 回答 2

1

Right, this is a bit of a guess, so apologies if I wind up wasting your time here...

It looks to me like in your create method, you're creating the 'wedding' relationship (which is only a 'pretend' relationship really, has it's using :through => :wedding_assignments), and then returning this. You're then asking rails to re-create these objects in your call to Person.new. My guess is that rails is getting confused by trying to create an object at the far side of a has_many :through without the intermediate object being present.

I would be tempted to restructure this a little (untested code!):

def new
  @person = Person.new
  @wedding = Wedding.new
  @wedding_assignment = WeddingAssignment.new
end

def create
  @person = Person.new(params[:person])
  @wedding = Wedding.new(params[:person])
  @assignment = WeddingAssignment.new(params[:wedding_assignment].merge({:person => @person}))
end

I've got a feeling this'll work until the last line. I suspect to get that to work you might need to use transactions:

def create
  @person = Person.new(params[:person])
  @wedding = Wedding.new(params[:person])
  ActiveRecord::Base.transaction do
    if @person.valid? && @wedding.valid?
      [@person,@wedding].each.save!
      @assignment = WeddingAssignment.new(params[:wedding_assignment].merge({:person => @person}))
      @assignment.save!
    end
  end
end

This ought to ensure that everything is created in the right order and IDs are available at the right times etc. Unfortunately though, it's a bit more complicated than your example, and does mean that you'll struggle to support multiple weddings.

Hope this helps, and doesn't wind up being a blind alley.

于 2010-10-18T22:20:06.523 回答
0

尝试将“Person.new”更改为“Person.create”,也许立即在数据库中创建记录将有助于关联。

于 2010-10-18T21:38:25.080 回答