這裏是我有什麼,想要實現:「屬於」和「有很多」通過?
我有2種型號Category
和Collection
我想這個協會:
Category
屬於Collection
Collection
有許多Category
但我只需要這個關聯幾條記錄。 Like 100. 所以我沒有看到任何理由爲其餘100 000條記錄創建列。
我想這沒有運氣:
class Category
has_many :category_collection
# This is not a valid option
belongs_to :collection, through: :category_collection
# And this will throw
has_one :collection, through: :category_collection
# ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have
# a has_one :through association 'Category#collection'
# where the :through association 'Category#category_collection' is
# a collection. Specify a has_one or belongs_to association
# in the :through option instead.
end
class Collection
has_many :category_collection
has_many :categories, through: :category_collection
end
class CategoryCollection
self.table_name = 'categories_collections'
belongs_to :category
belongs_to :collection
end
或者,也許整個想法是錯誤的,我應該沒用列堅守?
我認爲你所需要做的就是 - >'收集有很多:類別&'類別belongs_to:收集' – nik
@nik是的,在這種情況下,我將得到剩餘的99k記錄無用的列。請再閱讀一次問題。 –