我无法从seed.rb填充我的数据,当我使用rake db:seed
时没有任何反应。
我可以在Rails 5中使用这个结构吗?
Product.delete_all
Product.create!(title: 'CoffeeScript',
description:
%{<p>
CoffeeScript is JavaScript done right. It provides all of JavaScript\'s
functionality wrapped in a cleaner, more succinct syntax. In the first
book on this exciting new language, CoffeeScript guru Trevor Burnham
shows you how to hold onto all the power and flexibility of JavaScript
while writing clearer, cleaner, and safer code.
</p>},
image_url: 'cs.jpg',
price: 36.00)
# . . .
Product.create!(title: 'Programming Ruby 1.9 & 2.0',
description:
%{<p>
Ruby is the fastest growing and most exciting dynamic language
out there. If you need to get working programs delivered fast,
you should add Ruby to your toolbox.
</p>},
image_url: 'ruby.jpg',
price: 49.95)
Product.create!(title: 'Rails Test Prescriptions',
description:
%{<p>
<em>Rails Test Prescriptions</em> is a comprehensive guide to testing
Rails applications, covering Test-Driven Development from both a
theoretical perspective (why to test) and from a practical perspective
(how to test effectively). It covers the core Rails testing tools and
procedures for Rails 2 and Rails 3, and introduces popular add-ons,
including Cucumber, Shoulda, Machinist, Mocha, and Rcov.
</p>},
image_url: 'rtp.jpg',
price: 34.95)
答案 0 :(得分:0)
$ rails new seedtest
$ cd seedtest
$ rails g model product title description image_url price:decimal
$ rails db:migrate
puts 'start loading data'
copy and paste your loading code into db/seeds.rb
puts 'finish loading data'
$ rails db:seed
start Loading data
finish Loading data
$ rails db
sqlite> select * from products;
1|CoffeeScript|
CoffeeScript is JavaScript done right. It provides all of JavaScript's
functionality wrapped in a cleaner, more succinct syntax. In the first
book on this exciting new language, CoffeeScript guru Trevor Burnham
shows you how to hold onto all the power and flexibility of JavaScript
while writing clearer, cleaner, and safer code.
|cs.jpg|36|2017-02-11 10:44:38.648505|2017-02-11 10:44:38.648505
...
...
More data
.exit # when you've finished