【发布时间】:2010-11-21 20:09:20
【问题描述】:
我有一种情况,我想在 Rails 中制作“参数”模型;例如我想定义PrototypeRecipe,然后能够创建多个DerivedRecipe;也许一种衍生配方使用更多的糖,而另一种使用更少的鸡蛋或其他东西。关键点是我希望所有“派生”实例都从单个共享 PrototypeRecipe 继承属性,但能够进行本地修改。
理想情况下,我希望能够在原型上定义方法(例如,将购物清单放在一起),并让这些方法响应派生实例中的本地更改(因此,如果我指定 3 个鸡蛋而不是 2 个,我可以调用原型的make_shopping_list 函数,它会反映这一点)。
是否有现有的方法来完成这样的事情?这是迄今为止我能想到的最好的:
class Ingredient << ActiveRecord::Base
belongs_to :recipe, :polymorphic => true
# uuid => UUID String (for grouping ingredients which change between prototype and derived instances)
end
class PrototypeRecipe << ActiveRecord::Base
has_many :ingredients
def make_ingredient_list(derived_recipe = nil)
self.ingredients.map {|i| derived_recipe.nil? ? i : derived_recipe.ingredients.where(:ingredient_uuid => i.uuid).first }
end
end
class DerivedRecipe << ActiveRecord::Base
belongs_to :prototype_recipe
has_many :ingredients
def method_missing(sym, *args)
self.prototype_recipe.send( sym, *args, self)
end
end
我知道这段代码可以变得更简洁,我更想知道是否可以改进一般方法。基本思想是每种成分都有一个唯一的 ID。要修改原型配方,您只需创建DerivedRecipe 的实例,将其链接到原型,然后添加与原型成分之一具有相同 UUID 的成分。
【问题讨论】:
标签: ruby-on-rails inheritance prototype