# Beginner: How to use data from external Database

**URL:** <https://www.ruby-forum.com/t/beginner-how-to-use-data-from-external-database/125486>\
**Category:** Rails\
**Created:** [January 9, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/beginner-how-to-use-data-from-external-database/125486 "2008-01-09T15:55:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![styrmis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@styrmis](https://www.ruby-forum.com/u/styrmis)\
**Post date:** [January 9, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/beginner-how-to-use-data-from-external-database/125486/1 "2008-01-09T15:55:35Z")

</div>

Hi,

im new to RoR so sorry if the questions sounds stupid to you.

Im trying to build a tool for a browsergame.  
The game allows access to its database and the db looks like this:

table villages:  
`id` int(9) unsigned NOT NULL default ‘0’,  
`x` smallint(3) NOT NULL default ‘0’,  
`y` smallint(3) NOT NULL default ‘0’,  
`tid` tinyint(1) unsigned NOT NULL default ‘0’,  
`vid` int(9) unsigned NOT NULL default ‘0’,  
`village` varchar(20) NOT NULL default ‘’,  
`uid` int(9) NOT NULL default ‘0’,  
`player` varchar(20) NOT NULL default ‘’,  
`aid` int(9) unsigned NOT NULL default ‘0’,  
`alliance` varchar(8) NOT NULL default ‘’,  
`population` smallint(5) unsigned NOT NULL default ‘0’,  
UNIQUE KEY `id` (`id`)

the contents of the db is a map of the game with villages in it  
where one player (identified by his uid) may have several villages.

i have a usertable that looks like this:

table players:  
`id` int(9) unsigned NOT NULL default ‘0’,  
`username` varchar(20) NOT NULL default ‘’,  
`password` varchar(20) NOT NULL default ‘’,  
`uid` int(9) NOT NULL default ‘0’,

(where uid is the userid in the game)

i want to build a association betwen one user and his villages:

class Village \< ActiveRecord::Base  
belongs\_to :player,  
:foreign\_key =\> ‘uid’ # doesnt seem to work  
end

class Player \< ActiveRecord::Base  
has\_many :villages,  
:foreign\_key =\> ‘uid’  
end

The problem im having is that rails tries to match the villages by  
the id of the table players on not the uid and i dont know how to  
tell rails to not use the id but uid instead.

Any ideas?
