Reputation: 12693
I have made a small db/seeds.rb
file:
web = Website.find_or_create_by(id: 1) do |w|
w.website = "http://example.dev/"
w.banner_msg = "Hey! Banner message!"
w.signup_msg = "Wahey! Sign up message!"
w.bg_col = "#333"
w.txt_col = "#EEE"
w.btn_col = "#999"
end
pub = Publisher.find_or_create_by(id: 1) do |p|
p.phone = "021 111 1111"
p.website = web
end
User.find_or_create_by(id: 1) do |u|
u.email = '[email protected]'
u.password = 'password'
u.first_name = 'Joe'
u.last_name = 'Bob'
u.publisher = pub
end
User.create(id: 5, email: "[email protected]")
When I run rake db:seed
, the terminal simply accepts the command and presents a new line, no error message, no "rake abort!", nothing.
When I check the fields in Rails Console, there's nothing there. What's happening?
Upvotes: 1
Views: 3155
Reputation: 3130
You can use find_or_create_by!(attributes, &block)
which is like find_or_create_by
, but calls create!
so an exception is raised if the created record is invalid. APIDock
Upvotes: 5
Reputation: 12693
Ok I found out that
1) The colors were breaking, they are not allowed to be 3 digits here, had to be the full 6.
2) My chain of relations was wrong. I had user
has a publisher
, which has a website
, when the models were actually setup where website
has a publisher
which has a user
.
My annoyance here is there were absolutely no error messages. I had to copy and paste the contents of seeds.rb
into Rails Console, then all it said was rollback
, so I had to enter user.errors
to see the errors for the user
, and like wise for the other models.
Does anyone have suggestions on better error feedback for this kind of situation?
Upvotes: 0