running rake db:seed without duplicating data
Use find _ or _ create_by method
nomination_titles = [
'Репортаж',
'Новость',
'Статья',
'Интервью',
'Обзор'
]
nomination_titles.each do |title|
Nomination.find_or_create_by_title title
end
Written by Павел Калашников
Related protips
2 Responses
I tend to use Nomination.where({ title: title, another: attribute}).first_or_create
This allows for finding by multiple attributes and looks a little cleaner too in my opinion
over 1 year ago
·
As you wish :)
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#