Comments (5)
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.
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.
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.
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.
@jferris This was very helpful, thanks
from factory_bot_rails.
Related Issues (20)
- [Feature Request] Install Generator HOT 4
- Issues with custom error message translations HOT 21
- incompatible marshal file format (can't be read) HOT 4
- Factory not registered: "credit_card" HOT 13
- 5.0.x does not play well with Spring HOT 5
- factory_bot_rails 6.0 checklist HOT 2
- Invalid DateTime.now field on factory creation
- Multiple Tests Fail on Vanilla `bundle exec rake` HOT 2
- Model generation ignores --force-plural HOT 3
- Rails ignoring factory HOT 2
- Install fewer gems on Travis HOT 1
- Incompatibility with Rails 6.0.3? HOT 2
- Perform DB migration will make factory_bot looking for methods in the wrong place HOT 2
- Successive associations issue when migrating to rails 6.1 HOT 3
- Switch from Travis CI to GitHub Actions
- Duplicate factories problem HOT 5
- Automatically populate attributes and types when generating factories for existing models HOT 2
- Cannot create Factory with multiple traits got (ArgumentError Exception: wrong number of arguments (given 1, expected 0)) Ruby 3.0, Rails 6.1.72, factory_bot_rails 6.1.0 HOT 2
- Dead link in CONTRIBUTING.md HOT 2
- Release new version to allow upgrade to factory_bot 6.3.0 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from factory_bot_rails.