使用带有多个belongs_to的rails 5播种数据

时间:2017-10-16 11:16:43

标签: ruby-on-rails

class Gallery < ApplicationRecord
  belongs_to :artist

  include Filterable

  validates :user, presence: true

  belongs_to :user, optional: true

  resourcify
end

我如何能够为具有多个belongs_to的数据设定种子? 这是我的方法,但它给了我一个用户不能为空的错误

  

user = User.create! :name =&gt; 'Bobby Joe',:email =&gt; '20 @ gmail.com',:password =&gt; 'password',:password_confirmation =&gt; '密码'

     

artistOne = user.artists.create!(artist_name:'ED',first_name:'Edgar',last_name:'Degas',email:'edgardegas@yahoo.com',密码:'密码',街道:'2625 Ashcraft',城市:'圣地亚哥',州:'CA',邮政编码:'92103',网站:'www.edgardegas.com',销售:'',电话:'(760)210-1326')

     

galleryOne = artistOne.galleries.create!(名称:'艺术展',网站:'www.ExhibitionOfArt.com',电话:'(619)264-8402',开场时间:'10:00:00' ,关闭:'18:00:00',街道:'325 15th Street',城市:'圣地亚哥',州:'CA',邮编:'92101')

2 个答案:

答案 0 :(得分:0)

您需要添加您创建的关联(artistOne,user),

  

galleryOne = artistOne.galleries.create!(名称:&#39;艺术展览&#39;,网站:&#39; www.ExhibitionOfArt.com&#39;,电话:&#39;(619)264 -8402&#39;,开放时间:&#39; 10:00:00&#39;,关闭:&#39; 18:00:00&#39;,街道:&#39; 325 15th Street&#39;,city :&#39;圣地亚哥&#39;,州:&#39; CA&#39;,邮政编码:&#39; 92101&#39;,artist: artistOne, user: user

答案 1 :(得分: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)