播种与has_one,has_many和通过协会
问题描述:
我正在运行rake db:seed命令,它不会给出任何错误。但是,我没有通过控制台Event.find(1)
或通过服务器localhost:3000/events/
找到此事件。我知道我一定在某处搞乱了。 协会:播种与has_one,has_many和通过协会
class Event < ApplicationRecord
has_one :lineup
has_many :artists, :through => :lineup
belongs_to :venue
end
seed.rb:
Event.create(name: "The function",
date: DateTime.new(2016,2,3,10,0,0,'+7'),
venue: Venue.create(name: "Speakeasy", address: "Lynwood Ave", zip_code: "30312"),
lineup: Lineup.create(:artist => Artist.create(name: "DJ Sliink", bio: "jersey club king")),
description: "free free free")
答
这有可能是任何.create
方法默默地失败验证。
对象是否被成功保存到数据库或没有返回生成的对象。
尝试用create!
代替;如果记录无法保存,它会引发异常。
我会推荐在seeds.rb
的任何地方使用create!
来避免无声故障。
是的,这有帮助!事实证明,我已经将它建模为无法在没有“事件”的情况下创建“阵容” – sivanes