ruby on rails - Why does my FactoryGirl callback run when it shouldn't? -


I am using Factorigriel 3.3.0 with RoR 3.2.3

A I The user is the model that is as soon as the profile;

  class users & lt; ActiveRecord :: Base has_secure_password has_one: profile, depending destroyed accepts_nested_attributes_for: Profile, Update_only: true attr_accessible: Email ,: username ,: Password: password_confirmation ,: profile_attributes before_create: build_profile end class profile & lt; ActiveRecord :: Base Attr_accessible: First_name ,: Last_name Belongs_to: user's authentication: User presence: the true verification: First_name, appearance is true, at the update verification: Last_name presence: the truth, Updates end   

I Arspisi need to prevent the formation of me sometimes before the trials are run build_profile so I can be a profile without a user I do with it Faktrigrl callback Management I

 after  (: creation) {| User | User.class.skip_callback (: if: first ,: build_profile)}   

My user factories are defined as such;

 factoryGirl.define: User Sequence (: Email) {| N | "User_#{n}@example.com"} sequence (: username) {| N | "User _ # {n}"} password "secret" factory: user_with_profile factory: first to new_user_with_profile (create ()) {| User | User.activated = false} End Factory: Before activated_user_with_profile (): {| User | User.activated = true} End and Factory: after user_without_profile (: build) {| User | User.class.skip_callback (: Create, first,: build_profile)} factory: do new_user_without_profile (first ()) {| User | User.activated = false} End Factory: Before activated_user_without_profile (): {| User | user.activated = true} end end end end   

I was hoping that : new_user_without_profile and : activated_user_without_profile after (Construction) callback: user_without_profile while : not enough activated_user_with_profile factories, but it is not: new_user_with_profile and Such a thing is a part of the console here to demonstrate my problem;

  irb (main): 001: 0> User = FactoryGirl.create: new_user_with_profile irb (main): 002: 0> User.profile = & gt; # & Lt; Profile Id: 11, first_name: ", last_name:" ", created_at:" 2012-07-10 08:40:10 ", updated_at:" 2012-07-10 08:40:10 ", user_id: 18 & Gt; IRB (Main): 003: 0> User = FactoryGeral Create: new_user_without_profile irb (main): 004: 0> User.profile = & gt; Zero Yerb (Main): 005: 0> User = FactoryGeral Create: new_user_with_profile irb (main): 006: 0> User.profile = & gt; Therefore, for the first time I made one: new_user_with_profile, a profile is created as expected, but for the second time (a: after creating new_user_without_profile), it is not much more! The callback (but build) does not seem to be called again (if I add some code to some output, I do not see it in the terminal). I do not know what's going on wrong here. Is anyone else?   

This is a dirty solution, but you have tried to write the definition of callback factory: User_with_profile :

 after  (: creation) {| User | User.class.set_callback (: Create, first, build_profile)}   

Does this work?

Comments