![](/img/trans.png)
[英]Rails has_many :through and has_one :through associations
[英]seeding with has_one, has_many and through associations
我正在运行 rake db:seed 命令,它没有给出任何错误。 但是,我没有通过控制台Event.find(1)
或服务器localhost:3000/events/
找到此事件。 我知道我肯定在某个地方搞砸了。 协会:
class Event < ApplicationRecord
has_one :lineup
has_many :artists, :through => :lineup
belongs_to :venue
end
种子.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
文档:
无论对象是否成功保存到数据库,都会返回结果对象。
尝试使用create!
反而; 如果无法保存记录,它将引发异常。
我建议使用使用create!
在seeds.rb
随处可见,以避免无声失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.