# Belongs\_to + foreign\_key

**URL:** <https://www.ruby-forum.com/t/belongs-to-foreign-key/66717>\
**Category:** Rails\
**Created:** [August 28, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/belongs-to-foreign-key/66717 "2006-08-28T18:21:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [August 28, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/belongs-to-foreign-key/66717/1 "2006-08-28T18:21:22Z")

</div>

Having these models:

class League \< Tournament  
has\_many :league\_positions, :order =\> :position, :dependent =\>  
:destroy  
end

class LeaguePosition \< ActiveRecord::Base  
belongs\_to :league, :class\_name =\> ‘League’, :foreign\_key =\>  
‘tournament\_id’  
acts\_as\_list :scope =\> :tournament\_id  
end

when I try to destroy a League (so its LeaguePositions too) I get these  
SQL error:

Unknown column ‘league\_positions.league\_id’ in ‘where clause’: SELECT \*  
FROM league\_positions WHERE (league\_positions.league\_id = 1) ORDER BY  
position

I thought declaring :foreign\_key =\> ‘tournament\_id’ was enough to let  
league\_positions knows who  
is its owner. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [August 28, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/belongs-to-foreign-key/66717/2 "2006-08-28T18:57:33Z")

</div>

> acts\_as\_list :scope =\> :tournament\_id  
> end
> 
> when I try to destroy a League (so its LeaguePositions too) I get these  
> SQL error:
> 
> Unknown column ‘league\_positions.league\_id’ in ‘where clause’: SELECT \*  
> FROM league\_positions WHERE (league\_positions.league\_id = 1) ORDER BY  
> position

Does your League table have a field league\_positions\_id?

Does your LeaguePositions table have a field tournament\_id?

-Ben L.

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [August 28, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/belongs-to-foreign-key/66717/3 "2006-08-28T19:07:35Z")

</div>

Excuse me I was a little off. Your League table doesn’t need to have a  
league\_positions\_id.

The problem is that LeaguePositions knows that it is connected to  
Leagues by tournament\_id. But Leagues think that the key that connects  
a LeaguePositions to it is called league\_positions\_id.

this can be fixed in two ways  
#1  
class League \< Tournament  
has\_many :league\_positions, :order =\> :position, :dependent =\>  
:destroy, :class =\> ‘LeaguePositions’, :foreign\_key =\> ‘tournament\_id’  
end

OR

#2  
change the foreign key in LeaguePositions from being tournament\_id to  
being league\_id. Then change that line in the LeaguePositions class.

-Ben L.

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [August 29, 2006, 9:32am UTC](https://www.ruby-forum.com/t/belongs-to-foreign-key/66717/4 "2006-08-29T09:32:26Z")

</div>

> #1  
> class League \< Tournament  
> has\_many :league\_positions, :order =\> :position, :dependent =\>  
> :destroy, :class =\> ‘LeaguePositions’, :foreign\_key =\> ‘tournament\_id’  
> end

I’ve chosen the 2nd option, it works, but I had thought that declaring  
the :foreign\_key and  
:class\_name at belongs\_to relationship in LeaguePosition model was  
enough to tell the framework  
how the relationship is.

Thanks!
