Coder Social home page Coder Social logo

Comments (5)

jferris avatar jferris commented on June 3, 2024

Can you paste the person factory that's causing this issue? I think one possible workaround is to use Factory.define :person instead of Factory.define Person.

I think the issue is that simply referencing the Person class fails if the table doesn't exist yet.

from factory_bot_rails.

CodeSturgeon avatar CodeSturgeon commented on June 3, 2024

Sorry, I just tried to repro this and realized there is an additional step... one of the factories needs to reference another...

Steps to repro...

  • rails generate model Person name:string
  • rails generate model Hat type:string person_id:integer
  • rake db:migrate
  • vim app/model/hat.rb (belongs_to :person)
  • vim spec/factories.rb (see below)
  • rake db:drop
  • rake db:setup

factories.rb contents:

Factory.define :person do |p|
  p.name 'frank'
end

Factory.define :hat do |h|
  h.person Factory(:person)
end

Output...

db/test.sqlite3 already exists
db/test.sqlite3 already exists
db/development.sqlite3 already exists
rake aborted!
Could not find table 'people'

from factory_bot_rails.

jferris avatar jferris commented on June 3, 2024

Alright, I see the issue. You want to use a dynamic attribute, so that it generates a new person when each hat is created, instead of creating a person when the hat factory is defined:

Factory.define :hat do |h|
  h.person { Factory(:person) }
end

Does that make sense?

from factory_bot_rails.

CodeSturgeon avatar CodeSturgeon commented on June 3, 2024

HA! I just figured out this workaround myself and came to comment. Lazy attributes FTW.

I can see (after some thought) how this works and gets around the problem. However, it's not intuitive. Perhaps you should update the README so others don't stumble in to this trap as I did.

Thanks for the help and the gem in general!

from factory_bot_rails.

tsyber1an avatar tsyber1an commented on June 3, 2024

@jferris This was very helpful, thanks

from factory_bot_rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.