Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8214479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:28:37+00:00 2026-06-07T11:28:37+00:00

I am using FactoryGirl 3.3.0 with RoR 3.2.3 I have a user model which

  • 0

I am using FactoryGirl 3.3.0 with RoR 3.2.3

I have a user model which has_one profile like so;

class User < ActiveRecord::Base
  has_secure_password
  has_one :profile, dependent: :destroy
  accepts_nested_attributes_for :profile, update_only: true
  attr_accessible :email, :username, :password, :password_confirmation, :profile_attributes
  before_create :build_profile
end

class Profile < ActiveRecord::Base
  attr_accessible :first_name, :last_name
  belongs_to :user
  validates :user, presence: true
  validates :first_name, presence: true, on: :update
  validates :last_name, presence: true, on: :update
end

In my rspec tests I need to sometimes prevent the before_create :build_profile from running so I can have a user without a profile. I manage this with a FactoryGirl callback

after(:build) {|user| user.class.skip_callback(:create, :before, :build_profile)}

My user factories are defined as follows;

FactoryGirl.define do
  factory :user do
    sequence(:email) {|n| "user_#{n}@example.com"}
    sequence(:username) {|n| "user_#{n}"}
    password "secret"
    factory :user_with_profile do
      factory :new_user_with_profile do
        before(:create) {|user| user.activated = false}
      end
      factory :activated_user_with_profile do
        before(:create) {|user| user.activated = true}
      end
    end
    factory :user_without_profile do
      after(:build) {|user| user.class.skip_callback(:create, :before, :build_profile)}
      factory :new_user_without_profile do
        before(:create) {|user| user.activated = false}
      end
      factory :activated_user_without_profile do
        before(:create) {|user| user.activated = true}
      end
    end
  end
end

My expectation was that the :new_user_without_profile and :activated_user_without_profile would inherit the after(:build) callback from :user_without_profile while the :new_user_with_profile and :activated_user_with_profile factories would not, but it’s not quite working like that. Here’s an excerpt from the console to demonstrate my problem;

irb(main):001:0> user = FactoryGirl.create :new_user_with_profile
irb(main):002:0> user.profile
=> #<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>
irb(main):003:0> user = FactoryGirl.create :new_user_without_profile
irb(main):004:0> user.profile
=> nil
irb(main):005:0> user = FactoryGirl.create :new_user_with_profile
irb(main):006:0> user.profile
=> nil

So, the first time I create a :new_user_with_profile, a profile is created as expected but the second time (after creating a :new_user_without_profile), it doesn’t any more! The after(:build) callback doesn’t seem to be getting called again (if I add some code to it to output something, I don’t see it in the terminal). I have no idea what’s going wrong here. Does anyone else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:28:40+00:00Added an answer on June 7, 2026 at 11:28 am

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

    after(:build) {|user| user.class.set_callback(:create, :before, :build_profile)}
    

    Does it work?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following setup: Models: class Product < ActiveRecord::Base has_one :product_category attr_accessible :name,
Here are the model relationships: class Tool < ActiveRecord::Base ... has_many :loss_ratios, :dependent =>
I have a simple user factory that looks like this: FactoryGirl.define do factory :user
The design I have a User model that belongs to a profile through a
I have a category model. I am using ancestry gem to make it like
I have a Customer model that has_many polymorphic Address objects, like so: Customer Model:
I have these models that I'm trying to create factories for using factory_girl. class
I have a FActory using the factory_girl_rails gem 3.2 FactoryGirl.define do sequence(:random_string) {|n| #{LoremIpsum.generate}#{n}
I am using Ruby on Rails 3.0.9, RSpec-rails 2 and FactoryGirl. I would like
I am using Ruby on Rails 3.0.10, RSpec 2 and FactoryGirl. I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.