我无法从seed.rb填充数据

时间:2017-02-11 09:10:43

标签: ruby-on-rails ruby-on-rails-5 seed

我无法从seed.rb填充我的数据,当我使用rake db:seed时没有任何反应。

  • 使用Rails 5.
  • 来自Rails 4的数据

我可以在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)

1 个答案:

答案 0 :(得分:0)

种子数据加载和验证

创建测试应用程序

$ rails new seedtest
$ cd seedtest

添加产品型号

$ rails g model product title description image_url price:decimal
$ rails db:migrate

准备db / seeds.rb文件

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

Code on github - with commits using each title