# How do you make associations when a table has two unique columns?

**URL:** <https://discuss.rubyonrails.org/t/how-do-you-make-associations-when-a-table-has-two-unique-columns/42038>\
**Category:** rubyonrails-talk\
**Created:** [April 28, 2009, 7:38pm UTC](https://discuss.rubyonrails.org/t/how-do-you-make-associations-when-a-table-has-two-unique-columns/42038 "2009-04-28T19:38:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blgroup](https://avatars.discourse-cdn.com/v4/letter/b/eb8c5e/32.png) [@blgroup](https://discuss.rubyonrails.org/u/blgroup)\
**Post date:** [April 28, 2009, 7:38pm UTC](https://discuss.rubyonrails.org/t/how-do-you-make-associations-when-a-table-has-two-unique-columns/42038/1 "2009-04-28T19:38:13Z")

</div>

Having trouble with a somewhat unconventional modelling in Rails 2.2.2.

Take three table.

1. User model with primary integer id and a unique string guid.

2. Model X has foreign key to User as integer user\_id and its model includes belongs\_to :users

3. Model Y has foreign key to User as string user\_guid and its model includes belongs\_to :users, :foreign\_key =\> 'user\_guid'

This tells Rails which column to use in Model Y to look up Users. But How do I tell Rails which column to use in User?

In other words, how do I tell Rails to join (or select) from Users.guid instead of Users.id when referencing Y.user?

The has\_one and has\_many association has a :primary\_key option, but this seems to be missing on the belongs\_to association

How do people work around this? Any help would be greatly appreciated.

Thanks, =Blair.

---

<div class="post-metadata">

**Author:** ![Andrew\_Timberlake](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Andrew\_Timberlake](https://discuss.rubyonrails.org/u/Andrew_Timberlake)\
**Post date:** [April 29, 2009, 3:15am UTC](https://discuss.rubyonrails.org/t/how-do-you-make-associations-when-a-table-has-two-unique-columns/42038/2 "2009-04-29T03:15:40Z")

</div>

> Having trouble with a somewhat unconventional modelling in Rails 2.2.2.
> 
> Take three table.
> 
> 1. User model with primary integer id and a unique string guid.
> 
> 2. Model X has foreign key to User as integer user\_id and its model includes belongs\_to :users
> 
> 3. Model Y has foreign key to User as string user\_guid and its model includes belongs\_to :users, :foreign\_key =\> 'user\_guid'

Are you sure you don't mean: belongs\_to :user, :foreign\_key =\> 'user\_guid' #\<- Using singular 'user' then Y.user should work fine

> This tells Rails which column to use in Model Y to look up Users. But How do I tell Rails which column to use in User?
> 
> In other words, how do I tell Rails to join (or select) from Users.guid instead of Users.id when referencing Y.user?
> 
> The has\_one and has\_many association has a :primary\_key option, but this seems to be missing on the belongs\_to association
> 
> How do people work around this? Any help would be greatly appreciated.
> 
> Thanks, =Blair.

Setup your user model like this:

class User \< ActiveRecord::Base &nbsp;&nbsp;set\_primary\_key :guid end

Andrew Timberlake [http://ramblingsonrails.com](http://ramblingsonrails.com)[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

"I have never let my schooling interfere with my education" - Mark Twain
