简体   繁体   中英

seeding with has_one, has_many and through associations

I'm running the rake db:seed command and it doesn't give any errors. However, I'm not finding this Event through the console Event.find(1) or through the server localhost:3000/events/ . I know I'm definitely messing up somewhere. the associations:

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")

It's possible that any of the .create methods are silently failing validations.

From the create docs :

The resulting object is returned whether the object was saved successfully to the database or not.

Try using create! instead; it will raise an exception if the record could not be saved.

I would recommend using using create! everywhere in seeds.rb to avoid silent failures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM