# Foreign key re-assignment does not work in :belongs\_to

**URL:** <https://www.ruby-forum.com/t/foreign-key-re-assignment-does-not-work-in-belongs-to/156947>\
**Category:** Rails\
**Created:** [January 5, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/foreign-key-re-assignment-does-not-work-in-belongs-to/156947 "2009-01-05T17:49:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [January 5, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/foreign-key-re-assignment-does-not-work-in-belongs-to/156947/1 "2009-01-05T17:49:46Z")

</div>

I have a MS SQL database that I am accessing through ODBC. The tables  
do not conform to Rails defaults so I am having to re-assign the  
primary keys and foreign keys.  
I have the following:  
class DimPlayer \< ActiveRecord::Base

set\_primary\_key “player\_id”  
has\_many :map\_players

end

class MapPlayer \< ActiveRecord::Base

set\_primary\_key “map\_id”

belongs\_to :dim\_player, :foreign\_key =\> “to\_player\_id”

end

When I do a search on MapPlayer, I can use the association with no  
problem, e.g.  
player\_entries = MapPlayer.find(:all, :conditions =\> [“to\_player\_id  
= ?”, player\_id]) returns an array and elements of the array link  
back to the DimPlayer table as expected. (player\_entries  
[0].dim\_player)

When I try it the other way,  
dim\_player\_found = DimPlayer.find(:first, :conditions =\> [ “surname  
= ?”, “XXXX”])  
dim\_player\_found.map\_players gives me an error, such as the one below,  
which suggests I’m doing something wrong with the :foreign\_key =\>  
“to\_player\_id” line.  
The error is  
S0022 (207) [unixODBC][FreeTDS][SQL Server]Invalid column name  
‘dim\_player\_id’ which is what I would expect if I had not tried the  
foreign\_key re-assignment.  
I can’t see the wood for the trees - any help most appreciated!  
regards  
Martin

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [January 5, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/foreign-key-re-assignment-does-not-work-in-belongs-to/156947/2 "2009-01-05T18:00:55Z")

</div>

On 5 Jan 2009, at 16:49, Martin H. wrote:

> foreign\_key re-assignment.  
> You need to set the :foreign\_key option on the has\_many too.

Fred

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [January 5, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/foreign-key-re-assignment-does-not-work-in-belongs-to/156947/3 "2009-01-05T18:05:36Z")

</div>

Spot on, thanks. SO simple when you know how!  
Martin

On Jan 5, 4:58 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
