Problem with Factory_girl, association and after_initialize

前端 未结 3 1612
予麋鹿
予麋鹿 2021-02-07 02:12

I have a Family class so defined:

class Family < ActiveRecord::Base
  after_initialize :initialize_family
  belongs_to :user
  validates :user, 
       :prese         


        
3条回答
  •  太阳男子
    2021-02-07 02:20

    This is the answer I got from Joe Ferris:

    factory_girl doesn't pass arguments to the constructor. It uses #user= on your model, and instantiates it without any arguments.

    and this one from Ben Hughes:

    To elaborate on what Joe is saying, after_initialize methods are called immediately upon object initialization, and that time indeed user has not been set.

    So for example while this will work:

    family = Family.create!(:user => @user) # or @user.families.create ...
    

    This will not (which is what factory_girl is doing under the hood):

    family = Family.new
    family.user = @user
    family.save!
    

    Just in general you want to be real careful using after_initialize, as remember this is called on every object initialization. A Family.all call on 1,000 objects will cause that to get called 1,000 times.

    Sounds like in this instance you might be better of using a before_validation instead of after_initialize.

    The following syntax also works for testing in rspec:

    let (:family) { Family.create(:user => @user) }
    

提交回复
热议问题