The foreign key is the key to use in the OTHER table, usually called
ID.
The domestic key (my name; I don’t know the right formal term) is the
key to use in the calling table, usually called nameofothertable_id ,
i.e. photo_id or user_id.
I have a situation that appears to be surprisingly rare, although I
would think it would be quite common in businesses with legacy
tables. Let’s see if I can explain it.
I have a table called users.
It has a field called main_photo , which is the ID number of the photo
that is displayed with the user’s profile.
Many moons ago, when I started my project, I had real photos in the
photos table, and that made the photos table so huge that I would
never actually want to find the photo within the database. But I
changed things so that the photos are now stored in the file system,
because it’s much faster. So now all my photo table has is the
extension and various sizes, and I would like to pull it up like this:
user = User.find(1, :include => :photo)
but I cannot because it’s assuming a field called photo_id in the
users table, instead of the actual photo ID in main_photo.
Now, obviously I could change main_photo to photo_id but that would
require a horrendous testing and debugging cycle, affecting probably
hundreds of references, particularly since I have a method called
photo_id this would conflict directly with.
As far as I know, there’s actually no way to do this, but I figured I
would ask before giving up. It seems like it should be an easy thing
to do that someone would have already thought of, but tons of google
searches have yet to come up with any kind of clue.
Many thanks for any thoughts.
D