![](/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.