Forum: Ruby on Rails Using External Databases that Don't Have ID Columns

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.
Fd17e0a67191e9e72df285e1b8e4c2fb?d=identicon&s=25 Will Clark (willclarkdesigns)
on 2007-04-30 05:46
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"

Class Photo
belongs_to :property, :class_name => "Property", :foreign_key =>

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.
833bc92fe8f46a9bfebae5afb65f7507?d=identicon&s=25 Swanand Deodhar (swanand)
on 2007-04-30 10:35
(Received via mailing list)
  Following code should set the primary key to propNum.
   class Property
      set_primary_key 'propNum'
 Hope this helps,

 Thanks and regards,
Fd17e0a67191e9e72df285e1b8e4c2fb?d=identicon&s=25 Will Clark (willclarkdesigns)
on 2007-04-30 18:28
I knew it would be something really easy! Thanks Swanand!
This topic is locked and can not be replied to.