why model object.save triggers missing_method (rails 2.2)

Go To StackoverFlow.com

0

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>
2012-04-04 08:13
by Haseeb Ahmad
Do you know about the Rails #delegate method? IT may be useful to you: http://api.rubyonrails.org/classes/Module.html#method-i-delegat - joelparkerhenderson 2012-04-04 08:31
Yes one way to achieve this is to delegate methods to products model. But this way i had to delegate a huge list of methods. Then i thought to achieve it through method_missing. Which is unfortunately not working for m - Haseeb Ahmad 2012-04-04 09:05


1

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
2012-04-04 09:14
by John Plummer
Ads