ruby on rails 3 - FactoryGirl not respecting callback order -


broken case

i have following class

 class vehicle < activerecord::base      belongs_to :manufacturer      belongs_to :production_plant      before_save :delegate_audit_number       attr_accessible :manufacturer_id, :production_plant_id, :audit_number      private          def delegate_audit_number           self.audit_number ||= self.manufacturer.audits.last.try(:number)         end  end 

and following factory

  factorygirl.define     factory :vehicle       association :production_plant, factory: :production_plant       after(:validation) |v|         v.manufacturer_id = v.production_plant.manufacturer_id       end     end   end 

then calling factorygirl.create(:vehicle) fails in before_save with

          failure/error: @vehicle = factorygirl.create(:vehicle)              nomethoderror:                undefined method `audits' nil:nilclass 

by debugging, have discovered 2 things:

1) manufacturer method nil in before_save callback

2) after_validation callback never hit.

working case

identical in every way broken case, except before_save moved out observer

class auditnumberobserver < activerecord::observer   observe :vehicle    def before_save(entity)     entity.audit_number ||= entity.manufacturer.audits.last.try(:number)   end end 

now understanding (based on following list in documentation: http://guides.rubyonrails.org/v3.2.9/active_record_validations_callbacks.html#available-callbacks )

that after_validation should happen before before_save

why factory girl not respecting callback chain in first case? why factory girl respecting callback chain in second case? difference?

i think problem use of after(:validation) in factory.

according factorygirl's readme, callback not defined gem.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -