黄瓜重复类问题:AssociationTypeMismatch

时间:2011-02-22 02:42:13

标签: ruby-on-rails ruby-on-rails-3 cucumber

当模型回调方法中存在对self的引用时,我遇到了Cucumber的问题。

下面的代码示例。 错误如下: 人(#26738680)期待,有人(#29003170)(ActiveRecord :: AssociationTypeMismatch)

class Person < ActiveRecord::Base


  has_one :person_profile    
  accepts_nested_attributes_for : person_profile                             
  after_create :new_person_profile

  private 

  def new_person_profile
    person_profile = PersonProfile.new( 
      ...,
      :person => self  # <--- this causes the error in cucumber
    ) 
    self.person_profile.save
  end

失败的情况如下:

  Scenario: Student logs in for the first time
    Given I am a valid Student

失败的步骤:

Before do
  include Authlogic::TestCase
  activate_authlogic
end

def valid_person
    @current_person = Factory.create(:valid_person, :person_profile => new_person_profile('Kelly','Hope'))
end  

Given /^I am a valid Student$/ do
  valid_student
end

谢谢 亚当

1 个答案:

答案 0 :(得分:0)

这并没有直接回答你的问题,但我建议不要使用after_create回调。您已经在accepts_nested_attributes_for模型中定义了Person - 如果目标是在一个步骤中创建Person和关联的PersonProfile,您应该可以(例如):

params = { :person => { :name => "John Smith",  
 :person_profile_attributes => { :some_profile_attribute => "some_value" }}}
@person = Person.create!(params[:person])