[Rails] Re: Stack overflow problem
Eric Anderson
eric at afaik.us
Sat Jan 8 05:07:15 GMT 2005
Eric Anderson wrote:
> So I would GREATLY appreciate anyone that could clue me into why this
> problem is showing up so I could look for a work-around.
I think I found the problem after lots of code searching. It looks like
the :foreign_key option cannot be the same as the attribute name. This
is because when finding the associated class it called the raw field
name as a method to get the actual value of the field to search on. It
is somewhat equivalent to:
class User
def account_id
read_attribute "account_id"
end
def account
Account.find( account_id )
end
end
Of course the real code is generated and more dynamic than above, but
that is the essence of how it is working. If :foreign_key is equal to
the attribute name then you effectively have:
class User
# This method is created by method_missing()
def account
read_attribute "account"
end
def account
Account.find( account )
end
end
Obviously the first definition (which really doesn't exist but is being
created by method_missing) is irrelevant because the second definition
is real (through a module_eval) and therefore when the second method is
called it will keep recursively calling itself.
So the next question is now that I have found the problem how do we fix
it. It seems the obvious answer is to change line 338-340 of
associations.rb to the following:
association_finder = options[:conditions] ?
"#{association_class_name}.find_on_conditions(read_attribute(\"#{association_class_primary_key_name}\"),
\"#{options[:conditions]}\")" :
"#{association_class_name}.find(read_attribute(\"#{association_class_primary_key_name}\"))"
Email wrapping may mess this up but basically we want to make it use the
underlying read_attribute method instead of the public accessor method.
This shouldn't break compatibility too much. The only harm is if a user
has overwritten the account_id method to make it load a different object
from what the database says it should load. But I imagine this would be
rare (if used at all by anyone).
I'm going to make the changes on my system to see if it works. I would
appreciate any comments about better approaches or any comments about
possible problems related to this.
Thanks,
Eric
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 187 bytes
Desc: OpenPGP digital signature
Url : http://one.textdrive.com/pipermail/rails/attachments/20050108/fcf58407/signature.bin
More information about the Rails
mailing list