Reputation: 93296
I wonder, should I create models or migrations first in Rails?
I've been following BDD, testing first, then actual code. That has given me an insight of going from TOP to BOTTOM.
I have heard some people say: first models and their associations, then migrations to create the database for them. Migrations/tables adapt to models, not the other way around.
So does this mean it's smart to go for the TOP-DOWN approach here too, models (associations, validations) first, then migrations?
Upvotes: 2
Views: 1439
Reputation: 14679
Technically, with BDD, TDD red-green-refactor principles you would create the spec or feature first.
describe MyModel do
it "should exist"
end
Then run the the test and let it fail because you don't have a model MyModel,
class MyModel< ActiveRecord::Base
end
then write the model code and then run the test again. This time it will fail because there is no table name with that model name, at which point you write the migration. After the migration is run, you should be able to be at green (pending status in my rspec example) because the spec is just stubbed out.
Upvotes: 2
Reputation: 16435
The best practice is to create the migrations first, because then you won't forget that you have to check-in the migration code to the source control system for others in the team to use. But again, in reality it is easier to start off with models first.
Upvotes: 1
Reputation: 3866
since models without migrations are useless go on with the Model generator (both at once) but anyway you have to create the db schema in order to test the models
Upvotes: 0
Reputation: 115422
If you use the model generator that comes with Rails then models and migrations usually get created in one step. I'm not sure what you mean by the statement "migrations/tables adopt to models, not the other way around". Tables are where models store their data so both are integral.
Upvotes: 2