# Has\_many :through, expects "through" model to be belongs\_to

**URL:** <https://www.ruby-forum.com/t/has-many-through-expects-through-model-to-be-belongs-to/161639>\
**Category:** Rails\
**Created:** [February 28, 2009, 6:54am UTC](https://www.ruby-forum.com/t/has-many-through-expects-through-model-to-be-belongs-to/161639 "2009-02-28T06:54:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [February 28, 2009, 6:54am UTC](https://www.ruby-forum.com/t/has-many-through-expects-through-model-to-be-belongs-to/161639/1 "2009-02-28T06:54:14Z")

</div>

## Hi, I’m using has\_many and through relationship between two models Profile and User Profile.rb

has\_and\_belongs\_to\_many :users  
belongs\_to :community

## User.rb

has\_and\_belongs\_to\_many :profiles  
has\_many :communities, :through=\>profiles

When i try to do  
@user.communities, Rails is searching for foreign key of user in profile  
table, whereas profile and user have habtm relationship.  
How to solve this problem?

---

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [February 28, 2009, 8:16am UTC](https://www.ruby-forum.com/t/has-many-through-expects-through-model-to-be-belongs-to/161639/2 "2009-02-28T08:16:22Z")

</div>

suppose if the junction model is ProfileUser You can define like

Profile.rb

> * * *

has\_many :profile\_users  
has\_many :users, :through =\> :profile\_users

> 

User.rb

> * * *

has\_many :profile\_users  
has\_many :profiles, :through =\> :profile\_users

profile\_users.rb  
belongs\_to :user  
belongs\_to :profile

Now you can access like @user.profiles Is that what u want?

Sijo

---

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [February 28, 2009, 8:56am UTC](https://www.ruby-forum.com/t/has-many-through-expects-through-model-to-be-belongs-to/161639/3 "2009-02-28T08:56:47Z")

</div>

You can only go through has\_many associations.

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 28/02/2009, at 4:54 PM, Preethi S.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [February 28, 2009, 11:38am UTC](https://www.ruby-forum.com/t/has-many-through-expects-through-model-to-be-belongs-to/161639/4 "2009-02-28T11:38:20Z")

</div>

Sijo Kg wrote:

> suppose if the junction model is ProfileUser You can define like
> 
> Profile.rb
> 
> > * * *
> 
> has\_many :profile\_users  
> has\_many :users, :through =\> :profile\_users
> 
> > 
> 
> User.rb
> 
> > * * *
> 
> has\_many :profile\_users  
> has\_many :profiles, :through =\> :profile\_users
> 
> profile\_users.rb  
> belongs\_to :user  
> belongs\_to :profile
> 
> Now you can access like @user.profiles Is that what u want?
> 
> Sijo

## Thanks for your input. I’m able to do @user.profiles, that is not creating any problem. The problem is, User:

has\_many :profile\_users  
has\_many :profiles, through=\>:profile\_users  
has\_many :community, through=\>:profiles

## where community

has\_many :profiles  
has\_many :users , through=\>:profiles

## and profile

belongs\_to :community

## profiles\_users

belongs\_to :profile  
belongs\_to :user

## When do @user.communities, flow should be like this:

Join the profile and profile user table and get all the profiles that  
belong to the user and, from the profiles check the community it belongs  
to and then return the result(communities).

but am not getting the desired result because rails is not joining all  
the tables needed, it is checking for community\_id in profile\_users  
whereas it is in profile table.
