1

I've inherited a project in which an ActiveRecord model is giving me strange behaviour. This is the only model that's behaving this way, so far.

car = Car.first
car.respond_to?(:each) # true

There is no each method defined in Car that I can find.

I read in another post that you can find out who injected the method, so I gave it a shot:

car.method(:each) # raises "undefined method `each' for class `Car'

I want to use Car with draper but because my instance behaves like an Enumerable, draper treats it as such. Draper is not the issue, however.

What should I do to figure it out?

Community
  • 1
  • 1
Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69
  • Post your `Car` model code. – Andrew Marshall Dec 17 '12 at 01:30
  • Unfortunately, I can't post without editing most of it (which probably defeats the purpose) because I'm not allowed to. Is there another way we can find out? It's quite a large model, anyway, at 1,286 lines. – Ramon Tayag Dec 17 '12 at 01:38
  • 4
    Well, `respond_to?` can be overridden, and often is in order to get classes to behave consistently in the face of `method_missing`, but that's only a possible mechanism, not an explanation. Without code, there's not much we can do. – Mark Reed Dec 17 '12 at 01:43
  • 4
    If the file is too large, do a binary search on it. Delete almost all of it. Did that fix it? Undo, split the deleted part into two halves, delete one of them. If that helped, undo and delete the other. Otherwise, keep the half deleted. Split what's left into two halves, and so on. – Dmitry Dec 17 '12 at 01:50
  • Ok thanks folks will get back to you. – Ramon Tayag Dec 17 '12 at 01:51
  • I agree, we need to see your Car model. What does **Car.respond_to?(:each)** return? – yuяi Dec 17 '12 at 01:51
  • Your comments helped me find it! I used Dmitry's binary-search to figure out what caused it, and found the problem in a gem that the model is using. The gem, Mark Reed, overrides respond_to. – Ramon Tayag Dec 17 '12 at 02:24
  • 5
    If you've solved your problem, you should either post your answer below, which you can accept after 48 hours, or, if you think your question/answer are of no relevance to future visitors, you can delete your question entirely. Please don't only leave your answer in a comment. – Andrew Marshall Dec 17 '12 at 04:31

1 Answers1

0

The comments to my question helped me find it! I used Dmitry's binary-search to figure out what caused it, and found the problem in a gem that the model is using. The gem, Mark Reed, overrides respond_to. So the lesson is: if responds_to is funky, perhaps it's being overridden somewhere.

Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69