I have three models. 1- Product 2- Bike 3- Car
These models have polymorphic association. The Product model contains the common things of Bike and Car: like price, color, and etc.
Now I wanted to access the methods of Products directly through the objects of Car or bike like bike_obj.price
def method_missing(meth, *args, &blk)
product.send(meth, *args, &blk)
rescue NoMethodError
super
end
Now I am able to to achieve this
>> Car.last.price
=> 1000
But the problem is SAVE method of the Car model has stopped working. I dont know why it is going in method_missing when I do Car.last.save. It raises this exception
NoMethodError: undefined method `<=>' for #<Car:0x7fcdd39ef2c0>
You should override respond_to?
whenever you override method_missing
:
def respond_to?(method, include_private = false)
super || product.respond_to?(method, include_private)
end