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 7066215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:03:25+00:00 2026-05-28T05:03:25+00:00

I have a model similar to the following: class Foo attr_accessor :attribute_a # Really

  • 0

I have a model similar to the following:

class Foo
  attr_accessor :attribute_a  # Really an ActiveRecord attribute
  attr_accessor :attribute_b  # Also an ActiveRecord attribute

  def determine_attribute_b
    self.attribute_b = some_biz_logic(attribute_a)
  end
end

In FactoryGirl 1.3, I had a factory that looked like this:

Factory.define :foo do |foo|
  foo.attribute_a = "some random value"
  foo.attribute_b { |f| f.determine_attribute_b }
end

This worked just fine. attribute_b was an attribute dependent on the code block, which would be passed a real instance of Foo into the variable f, complete with a properly set attribute_a to work off of.

I just upgraded to FactoryGirl 2.3.2, and this technique no longer works. The f variable from the equivalent code to above is no longer an instance of Foo, but instead a FactoryGirl::Proxy::Create. This class appears to be able to read the attributes previously set (such that the Dependent Attributes example in the docs still works). However, it can’t call actual methods from the built class.

Is there a way I can use the technique from the old version of FactoryGirl? I want to be able to define an attribute and set its value using the result of an instance method of the built class.

  • 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-05-28T05:03:25+00:00Added an answer on May 28, 2026 at 5:03 am

    You can use the callbacks after_create and after_build like this:

    FactoryGirl.define do
      factory :foo do
        attribute_a "some random value"
        after_build do |obj|
          obj.attribute_b = obj.determine_attribute_b
        end
      end
    end
    

    Keep in mind this new FactoryGirl syntax.

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

Sidebar

Related Questions

I have models similar to the following: class Band(models.Model): name = models.CharField(unique=True) class Event(models.Model):
I have a model similar to the following: class Review(models.Model): venue = models.ForeignKey(Venue, db_index=True)
I have two Django model classes that are structured similar to the following: class
I have a Model similar to the following public class TaskModel { public int
I have a model similar to this: public class myModel { public ClassA ObjectA
I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I
I have a class Similar to this public class Model { public TimeSpan Time1
I have the following model class Plugin(models.Model): name = models.CharField(max_length=50) # more fields which
I have the following model: public class Model : INotifyPropertyChanged { public ObservableCollection<ViewElement> Elements
I have a model like the following: public class Employee { public Employee() {

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.