Don't let seed data fail silently in rails
When developing with rails, use the create!
function rather than the create
function in your seeds.rb
file. Since model creation should never fail when you're seeding the database, using create!
will throw an error when the model cannot be saved to the database, whereas create
will fail silently and maybe produce mind bending bugs.
Do this:
User.create!(:username => 'user1', :password => 'password')
Not this:
User.create(:username => 'user1', :password => 'password')
Written by Noah Gilmore
Related protips
3 Responses
Your "Do this" and "Not this" are identical. Probably should drop the ! from the second one.
over 1 year ago
·
User.create!( username: 'user1', password: 'password')
over 1 year ago
·
Thanks markjreed, you're correct!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#