# Question about AR associations (belongs\_to)

**URL:** <https://www.ruby-forum.com/t/question-about-ar-associations-belongs-to/81943>\
**Category:** Rails\
**Created:** [January 16, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/question-about-ar-associations-belongs-to/81943 "2007-01-16T13:16:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [January 16, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/question-about-ar-associations-belongs-to/81943/1 "2007-01-16T13:16:03Z")

</div>

Hi !

A simple question about belongs\_to…

Two models : Upload and Uploadedfile.

In Upload :

belongs\_to :uploadedfile, :foreign\_key =\> “file\_number\_id”

The primary key of Uploads is id, and there is a column named number.

When I try :

upload = Upload.find(1)  
upload.uploadedfile

Rails do :  
SELECT \* FROM uploadedfiles WHERE (uploadedfiles.id = 255)

(255 is the value in file\_number\_id of course)

But I want Rails to do :  
SELECT \* FROM uploadedfiles WHERE (uploadedfiles.number = 255)

My question :

In belongs\_to, Rails have got a parameter to name the foreign\_key in the  
current model, but why Rails does not have a parameter to name the  
column in the other?  
In some case, the column I want to search is not the primary key!

For example I could write :

belongs\_to :uploadedfile, :foreign\_key =\> “file\_number\_id”, :column =\>  
“number”  
and it would send :  
SELECT \* FROM uploadedfiles WHERE (uploadedfiles.number = 255)

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [January 16, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/question-about-ar-associations-belongs-to/81943/2 "2007-01-16T15:59:12Z")

</div>

Nicolas B. wrote:

> Hi !
> 
> A simple question about belongs\_to…
> 
> Two models : Upload and Uploadedfile.

Hmmm 🙂

So a few things to note:

1. the belongs\_to goes in the ‘child’ record. ie. the record with the  
foreign key. Person belongs\_to :team ( the person table would have id  
and team\_id )

I’m thinking you should probably have UploadedFile belongs\_to :upload  
and have an upload\_id in UploadedFile.

1. Associations work like foreign keys, and so generally should ‘point’  
to a primary key (usually ‘id’).

So with tables:

uploads( id, number, name, whatever)  
uploaded\_files( id, upload\_id, filename, whatever)

and models

Upload  
has\_one :uploaded\_file

UploadedFile  
belongs\_to :upload

You should be set…I think.

Does this help ?

Alan

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [January 16, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/question-about-ar-associations-belongs-to/81943/3 "2007-01-16T16:01:14Z")

</div>

Alan F. wrote:

> So with tables:
> 
> uploads( id, number, name, whatever)  
> uploaded\_files( id, upload\_id, filename, whatever)
> 
> and models
> 
> Upload  
> has\_one :uploaded\_file
> 
> UploadedFile  
> belongs\_to :upload

I forgot to add 🙂

You can now do

@upload = Upload.find\_by\_number( aNumber )  
@upload.uploaded\_file

or

@upload = Upload.find(1)  
@upload.uploaded\_file

A.
