I have a table called recipes and another called ingredients. Ingredients can either connect to foods or recipes (one recipe can contain other recipes). I have implemented it with the food part of it but I'm not sure how to implement the recipe part of it. I created another table called the food part of it called recipe_as_ingredients which I will neet to populate when a recipe is saved with the recipes selected as ingredients in the current recipe. I would probably have to update the table before each save and manually delete lines if deleted for the recipes. Is there a better way of implemnting it?
Here are the models:
class Recipe < ActiveRecord::Base
has_many :ingredients
end
class Ingredient < ActiveRecord::Base
belongs_to :element, :polymorphic => true
belongs_to :recipe
end
class Food < ActiveRecord::Base
has_many :ingredients, :as => :element
end
class RecipeAsIngredient < ActiveRecord::Base
has_many :ingredients, :as => :element
end
I manually set the element type in a function before saving so I would need to to similar for a recipe as ingredient but make sure that recipe_as_ingredient contains a record to connect to the ingredient:
self.element_id = numid.id
self.element_type = 'Food'
p.s. I could not connect the recipe without the recipe_as_ingredients connection tables because it made saving the record impossible because recipe was both a master record and detail record and the id field was always nil.