Forum: Ruby on Rails Defining has_many without a foreign key

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
6ec7697ec27284826a651f7a603d1825?d=identicon&s=25 Norman Elton (normelton)
on 2008-11-19 21:31
(Received via mailing list)
Is it possible to have a has_many relationship without a foreign key?
I'd like to eager-load associations based on some arbitrary SQL. For
instance, I would like to do something like:

:has_many :neighbors, :class_name => "Person", :conditions =>
["street_id=?", self.street_id]

I believe this would work, but it would still look for a foreign key.
I know I could do this with named_scopes, or defining some method. But
I'd like to eager-load all the neighbors at once.

Thoughts? Thanks!

This topic is locked and can not be replied to.