# Rails - associations help

**URL:** <https://www.ruby-forum.com/t/rails-associations-help/187717>\
**Category:** Rails\
**Created:** [April 28, 2010, 7:35am UTC](https://www.ruby-forum.com/t/rails-associations-help/187717 "2010-04-28T07:35:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanjayarun07](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@sanjayarun07](https://www.ruby-forum.com/u/sanjayarun07)\
**Post date:** [April 28, 2010, 7:35am UTC](https://www.ruby-forum.com/t/rails-associations-help/187717/1 "2010-04-28T07:35:52Z")

</div>

## Hi I have two models . Dbase

name  
vendor  
type  
port  
defuser - default user name

## Users

username  
password  
email

I need to build an association between these two in that the dbase  
model’s defuser needs to be present in the users table. I am from a  
relational database background so am trying hard to understand rails  
associations.  
What should I do to associate the dbase’s defuser - \> users.username?  
I read the documents and everything was based on the column ‘id’.

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![sanjayarun07](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@sanjayarun07](https://www.ruby-forum.com/u/sanjayarun07)\
**Post date:** [April 28, 2010, 10:54am UTC](https://www.ruby-forum.com/t/rails-associations-help/187717/2 "2010-04-28T10:54:18Z")

</div>

Are you trying to link Dbase to a user as defuser (default\_user)?  
If so, this would work

class Dbase \< ActiveRecord::Base  
belongs\_to(:defuser, :class\_name =\> ‘User’, :foreign\_key =\>  
‘defuser’)  
end

It would be better rails practice to call the column  
“default\_user\_id”, and then make the association like this:

class Dbase \< ActiveRecord::Base  
belongs\_to(:default\_user, :class\_name =\> ‘User’)  
end

Btw, the column name “type” is a reserved rails. It is used for single  
table inheritance.  
Unless you are using single table inheritance here, I suggest you  
rename it to dbase\_type, otherwise you may encounter strange error  
messages.

---

<div class="post-metadata">

**Author:** ![sanjayarun07](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@sanjayarun07](https://www.ruby-forum.com/u/sanjayarun07)\
**Post date:** [April 29, 2010, 4:10am UTC](https://www.ruby-forum.com/t/rails-associations-help/187717/3 "2010-04-29T04:10:32Z")

</div>

Thanks for the code and also for the note. I’ve changed the var to  
db\_type. Also, what is the difference between having the :foreign\_key  
=\> ‘defuser’ and not, in your case of the first code and the 2nd?

Also, is there an IRC channel for rails?
