我有三个型号。1-产品2-自行车3-汽车
这些模型具有多态关联。Product模型包含了自行车和汽车的共同之处:比如价格、颜色等。
现在我想通过汽车或自行车的对象直接访问产品的方法,比如bike_obj.price
def method_missing(meth, *args, &blk)
product.send(meth, *args, &blk)
rescue NoMethodError
super
end现在我能够做到这一点
>> Car.last.price
=> 1000但问题是,汽车模型的保存方法已经停止工作。我不知道为什么当我做Car.last.save的时候它会变成method_missing。它会引发此异常
NoMethodError: undefined method `<=>' for #<Car:0x7fcdd39ef2c0>发布于 2012-04-04 17:14:46
每当覆盖method_missing时,都应该覆盖respond_to?
def respond_to?(method, include_private = false)
super || product.respond_to?(method, include_private)
endhttps://stackoverflow.com/questions/10007268
复制相似问题