将数据对播种到rails app

时间:2012-05-18 21:54:07

标签: mysql ruby-on-rails ruby-on-rails-3 activerecord

我试图将一个:name,:date对播放到我的rails app中的一个表(通过seed.rd)...不太确定如何做到这一点..我的尝试如下:

[[ "New Years Day", 2013/01/01 ][ "Easter", 2013/03/31]].each do |holiday|
  Holiday.create!({:name, :date => holiday})
end

1 个答案:

答案 0 :(得分:2)

选项1:

[{ :name => "New Years Day", :date => "2013/01/01" },{ :name => "Easter", :date => "2013/03/31"}].each do |holiday|
  Holiday.create!(holiday)
end

选项2:

[[ "New Years Day", "2013/01/01" ],[ "Easter", "2013/03/31"]].each do |holiday|
  Holiday.create!({:name => holiday[0], :date => holiday[1]})
end

选项3(实际上并不是一个好的选项,因为当你超越名字和日期时它会变脏):

{ "New Years Day" => "2013/01/01" , "Easter" => "2013/03/31"}.each do |holiday|
  Holiday.create!({:name => holiday.first, :date => holiday.second})
end