如何为与另外2个表匹配的中间表创建种子数据?

时间:2014-11-05 10:02:49

标签: ruby-on-rails seed

我有2个模特,照片和专辑。

为了匹配它们,我做了一个中间表Photo_Listings,其中包含photo_idalbum_id列。

但是,当尝试播种ddbb时,我遇到了以下问题:

NoMethodError: undefined method `photo_listing' for #<Album:0x00000109698318>

在我的种子中,我想创建一些专辑,还有一些照片列表,以便专辑里面有一些照片。

  15.times do
    album = Album.create do |w|
      w.title = Faker::Commerce.product_name
      w.description = Faker::Lorem.sentence
      w.user_id = rand(1..4)
    end
    album.photo_listing.create do |w|
      w.photo_id = rand(1..10)
      w.album_id = rand(1..10)
    end
  end

我的Photo_listings模型

class PhotoListing < ActiveRecord::Base
  belongs_to :album
  belongs_to :photo
end

1 个答案:

答案 0 :(得分:0)

试试这个:

您需要将表格调用为plural,而不是singular

album.photo_listing更改为album.photo_listings

它会起作用。

相关问题