Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5739 (new defect)

Opened 2 years ago

Last modified 2 years ago

timeout cannot reliably be used as an ActiveRecord attrribute/field name

Reported by: tve-ticket@voneicken.com Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version:
Severity: normal Keywords:
Cc:

Description

I had a hell of a time fiding out why I would intermittently get problems with an attribute/field named "timeout" in an ActiveRecord class. The symptoms are that sometimes I get a "wrong arg count (1 for 2)" error when trying to access the timeout attribute on an object. The reason this fails is that ruby/lib/ruby/1.8/timeout.rb defines a global method called timeout.

The reason this case was so difficult to track down is that if you have a table with two fields, say :name and :timeout and you have generate_read_methods == true, then the read accessor methods actually only get generated by the first call to :missing_method. So if you first access the :name attribute, :missed_method gets triggered and it defines methods for name(), name=(val), timeout(), and timeout=(val). So if you now access the :timeout attribute it's all fine. But if the first attribute accessed is :timeout, then you end up with a "wrong arg count" error when ruby tries to invoke the global timeout function instead of triggering a :missing_method exception.

I see two solutions to this: 1_ Define the attribute accessor methods earlier, e.g. when the object gets instantiated. I don't know any of that code, so I really can't tell what the difficulties would be.

2_ When the first object for a table gets instantiated, check whether any of them clash with existing methods, e.g. using the respond_to?() method and print a warning if there is a clash.

All this still leaves me with a question, which is how to debug such a situation. Even after setting a breakpoint in the debugger at a point where the object in question is in scope, how can I figure out what gets called for timeout()? There oesn't seem to be any way to ask for the file:line of the method (something like o.method(timeout).file_line).

Otherwise I enjoy Rails a lot: thanks much!!!

Change History

08/06/06 22:05:30 changed by tve-ticket@voneicken.com

  • summary changed from timout cannot reliably be used as an ActiveRecord attrribute/field name to timeout cannot reliably be used as an ActiveRecord attrribute/field name.

Sorry, typo in summary: "timeout" not "timout"...