# Belongs\_:to association

**URL:** <https://www.ruby-forum.com/t/belongs-to-association/172839>\
**Category:** Rails\
**Created:** [July 28, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/belongs-to-association/172839 "2009-07-28T19:52:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cocozz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@cocozz](https://www.ruby-forum.com/u/cocozz)\
**Post date:** [July 28, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/belongs-to-association/172839/1 "2009-07-28T19:52:39Z")

</div>

class Shopcategory0 \< ActiveRecord::Base  
set\_table\_name “shopcategories\_0”

has\_many :shopcategory1s, :foreign\_key =\> :parentcategory\_id  
end

class Shopcategory1 \< ActiveRecord::Base  
set\_table\_name “shopcategories\_1”

belongs\_to :shopcategory0, :foreign\_key =\> :shopcategory\_id  
end

class Shopcategory0sController \< ApplicationController  
def show  
@shopcategory = Shopcategory0.all (:joins  
=\> :shopcategory1s)  
end  
end

* * *

## That’s the SQL result:

Shopcategory0 Load (0.1ms) SELECT `shopcategories_0`.\* FROM  
`shopcategories_0` INNER JOIN `shopcategories_1` ON  
shopcategories\_1.parentcategory\_id = shopcategories\_0.id

* * *

## And this is what I search (notice the end):

Shopcategory0 Load (0.1ms) SELECT `shopcategories_0`.\* FROM  
`shopcategories_0` INNER JOIN `shopcategories_1` ON  
shopcategories\_1.parentcategory\_id = shopcategories\_0.shopcategory\_id

I suppose there is a way to do that…I’ll appreciate your help !!!

---

<div class="post-metadata">

**Author:** ![cocozz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@cocozz](https://www.ruby-forum.com/u/cocozz)\
**Post date:** [July 28, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/belongs-to-association/172839/2 "2009-07-28T20:06:42Z")

</div>

On Jul 28, 10:34 am, cocozz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## end
> 
> `shopcategories_0` INNER JOIN `shopcategories_1` ON  
> shopcategories\_1.parentcategory\_id = shopcategories\_0.shopcategory\_id
> 
> I suppose there is a way to do that…I’ll appreciate your help !!!

Is there a question in there?

-e

---

<div class="post-metadata">

**Author:** ![cocozz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@cocozz](https://www.ruby-forum.com/u/cocozz)\
**Post date:** [July 28, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/belongs-to-association/172839/3 "2009-07-28T20:11:59Z")

</div>

Of course !

Look at the end:

"  
class Shopcategory0 \< ActiveRecord::Base  
set\_table\_name “shopcategories\_0”

has\_many :shopcategory1s, :foreign\_key =\> :parentcategory\_id  
end

class Shopcategory1 \< ActiveRecord::Base  
set\_table\_name “shopcategories\_1”

belongs\_to :shopcategory0, :foreign\_key =\> :shopcategory\_id  
end

class Shopcategory0sController \< ApplicationController  
def show  
@shopcategory = Shopcategory0.all (:joins  
=\> :shopcategory1s)  
end  
end

* * *

## That’s the SQL result:

Shopcategory0 Load (0.1ms) SELECT `shopcategories_0`.\* FROM  
`shopcategories_0` INNER JOIN `shopcategories_1` ON  
shopcategories\_1.parentcategory\_id = shopcategories\_0.id

* * *

## And this is what I search (notice the end):

Shopcategory0 Load (0.1ms) SELECT `shopcategories_0`.\* FROM  
`shopcategories_0` INNER JOIN `shopcategories_1` ON  
shopcategories\_1.parentcategory\_id = shopcategories\_0.shopcategory\_id  
"

---

<div class="post-metadata">

**Author:** ![cocozz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@cocozz](https://www.ruby-forum.com/u/cocozz)\
**Post date:** [July 29, 2009, 1:26am UTC](https://www.ruby-forum.com/t/belongs-to-association/172839/4 "2009-07-29T01:26:20Z")

</div>

According to you, you are stating that…

shopcategories\_0 table has both and id column and a shopcategory\_id  
column.

You want to reference a foreign key through that table…

You probably need to use…

class Shopcategory1 \< ActiveRecord::Base  
set\_table\_name “shopcategories\_1”

belongs\_to :shopcategory0, :class =\> :shopcategory0, :foreign\_key =\>  
:shopcategory\_id  
end

You should really use the class for the model that houses the  
shopcategory\_id. If that model is the :shopcategory0 then it appears  
you aren’t normalizing your tables properly and you might want to  
rethink your design a bit. Also, your table names are very confusing.

---

<div class="post-metadata">

**Author:** ![cocozz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@cocozz](https://www.ruby-forum.com/u/cocozz)\
**Post date:** [July 28, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/belongs-to-association/172839/5 "2009-07-28T21:45:39Z")

</div>

2009/7/28 cocozz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end  
> =\> Â :shopcategory1s)
> 
> > On Jul 28, 10:34Â am, cocozz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Â set\_table\_name “shopcategories\_1”
> > 
> > > * * *
> > 
> > > Shopcategory0 Load (0.1ms) Â SELECT `shopcategories_0`.\* FROM  
> > > `shopcategories_0` INNER JOIN `shopcategories_1` ON  
> > > shopcategories\_1.parentcategory\_id = shopcategories\_0.shopcategory\_id
> > 
> > > I suppose there is a way to do that…I’ll appreciate your help !!!
> > 
> > Is there a question in there?

I too have difficulty seeing what is the question you are asking. Do  
you mean you are trying to generate that particular query? What does  
‘notice the end’ mean? If so why are you trying to generate some  
particular sql. Generally you should think about what you want to  
achieve at a higher level and let Rails worry about the query.

Colin
