2017-10-16 24 views
0
class Gallery < ApplicationRecord 
    belongs_to :artist 

    include Filterable 

    validates :user, presence: true 

    belongs_to :user, optional: true 

    resourcify 
end 

我將如何種子具有多個belongs_to的數據? 這是我的方法,但它給了我一個錯誤,用戶不能爲空有多個rails5播種數據belongs_to

user = User.create! :name =>'Bobby Joe',:email => '20 @ gmail.com',:password =>'password',:password_confirmation =>'password'

artistOne = user.artists.create!(artist_name :'ED',first_name:'Edgar',姓氏:'Degas',電子郵件:'[email protected]',密碼:'密碼',街道:'2625 Ashcraft',城市:'聖地亞哥',州:'' CA',郵編:'92103',網站:'www.edgardegas.com',銷售:'',電話:'(760)210-1326')

galleryOne = artistOne.galleries.create!(name: '藝術展',網站:'www.ExhibitionOfArt.com',電話:'(619)264-8402',開幕式:'10:00:00',結束時間爲'18:00:00',街道:'' 325 15th Street',城市:'聖地亞哥',州:'CA',郵政編碼:'92101')

+0

你有藝術家模型中的字段user_id嗎? – Gabbar

回答

0

您需要添加您創建的關聯(artistOne,用戶),

galleryOne = artistOne.galleries.create(名稱: '藝術展',網址: 'www.ExhibitionOfArt.com',電話:'(619)264-8402',開幕時間:'10:00:00',結束時間:'18:00:00',街道:'第15街道'325,城市:'聖地亞哥',州:'CA' ,郵編: '92101',artist: artistOne, user: user

0

你必須設置你的關係ID在你的畫廊創作這樣的:

galleryOne = artistOne.galleries.create!(name: 'Exhibition of Art', website: 'www.ExhibitionOfArt.com', phone: '(619)264-8402', opening:'10:00:00', closing:'18:00:00', street:'325 15th Street', city:'San Diego', state:'CA', zipcode: '92101', user_id: user.id, artist_id: artistOne.id)