你想爲每個項目一個表,併爲協會
#class RawComponent < ActiveRecord::Base
# has_and_belongs_to_many :recipes
#end
class Recipe < ActiveRecord::Base
has_many :recipe_components
has_many :subrecipes, :through => :recipe_components
has_many :recipes, :through => :recipe_components
# has_and_belongs_to_many :raw_components
end
class RecipeComponents < ActiveRecord::Base
belongs_to :recipe
belongs_to :subrecipe, :class_name => :Recipe
end
假設你有一個@recipe一個表,你可以TH en go:
@recipe.subrecipes # find all subrecipes required to make this recipe
@recipe.recipes # find all recipes using this as a subrecipe
還添加了可能的RawComponent類,您可以將它用於非由其他組件組成的事物。但是,如果您將每個RawComponent製作成沒有任何子配方的配方,則不需要它,這也是模擬情況的有效方法。
關鍵問題是,關聯模型(RecipeComponents)屬於層次結構中較高的配方,以及層次結構中較低但與配方具有相同類類型的子配方。
這個問題沒有任何意義。你能解釋你想做什麼嗎? – 2012-08-17 01:25:05