On Sat, 2008-03-22 at 21:28 -0700, David H Dennis wrote:> 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.
----
http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001105
Normally, you would have in the model...
Class User < ActiveRecord::Base
belongs_to :main_photo
and
Class MainPhoto < ActiveRecord::Base
belongs_to :user
and you would have a field called :main_photo_id in users table
and you would have a field called :user_id in main_photos table
This doesn''t require any extra effort.
If you want the field in users table to be called photo_id, then you
would have to declare it in User Class...
belongs_to :main_photo, :foreign_key => ''photo_id''
as described in the above link.
More importantly though, you probably want to use a plugin such as
''file_column'' to manage the links to the picture files and
then you can
just store the file name in your users table and not have a second table
for this purpose at all.
Craig
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---