簡體   English   中英

使用 has_one、has_many 和通過關聯進行播種

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM