I have to connect to an external database which I don''t have control of. They were not setup with a Rails application in mind. I can extract information easily enough, but creating associations between the models is my problem. I am not certain how exactly to let Rails know what the primary key column is. I have two tables, properties and photos. I would like to create an association like this: Class Property has_many :photos, :class_name => "Photo", :foreign_key => "propNum" end Class Photo belongs_to :property, :class_name => "Property", :foreign_key => "propNum" end The properties table uses ''propNum'' instead of ID. The photos table uses photoNum instead of ID, and associates to the properties table using a propNum column versus property_id. I am not certain how exactly to specify what the primary column name is so Rails can understand the association. Any help would be great. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
swanand deodhar
2007-Apr-30 08:33 UTC
Re: Using External Databases that Don''t Have ID Columns
Hi! Following code should set the primary key to propNum. class Property set_primary_key ''propNum'' end Hope this helps, Thanks and regards, Swanand On 4/30/07, Will Clark <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I have to connect to an external database which I don''t have control of. > They were not setup with a Rails application in mind. I can extract > information easily enough, but creating associations between the models > is my problem. I am not certain how exactly to let Rails know what the > primary key column is. > > I have two tables, properties and photos. I would like to create an > association like this: > > Class Property > has_many :photos, :class_name => "Photo", :foreign_key => "propNum" > end > > Class Photo > belongs_to :property, :class_name => "Property", :foreign_key => > "propNum" > end > > The properties table uses ''propNum'' instead of ID. The photos table uses > photoNum instead of ID, and associates to the properties table using a > propNum column versus property_id. > > I am not certain how exactly to specify what the primary column name is > so Rails can understand the association. Any help would be great. > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I knew it would be something really easy! Thanks Swanand! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---