# Associations In rails

**URL:** <https://www.ruby-forum.com/t/associations-in-rails/166280>\
**Category:** Rails\
**Created:** [April 23, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/associations-in-rails/166280 "2009-04-23T12:52:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 23, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/associations-in-rails/166280/1 "2009-04-23T12:52:55Z")

</div>

Hi,  
I have these Models in my app  
User  
Request  
Requesttargetuser

Now User has the associations  
has\_many :requests

And the Request has the association  
belongs\_to :user

ie.A user can post a request.

But the request should be answered by certain users ie.I have to add  
certain other users to reply for the request.

For that only I have this model called Requesttargetuser  
I know i can use  
in User model  
has\_many :requesttargetuser  
has\_many :requests :through=\> :requesttargetuser

But my question is that can i have both has\_many :requests and has\_many  
:requests :through =\> :requesttargetuser in the User table.Or else wat  
will be the correct way to address this situation.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 23, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/associations-in-rails/166280/2 "2009-04-23T14:14:25Z")

</div>

I don’t know if I got your question right but what if you just:

User model:  
has\_and\_belongs\_to\_many :requests

Request model:  
has\_and\_belongs\_to\_many :users

and create a habtm table like:  
create\_table :users\_requests, :id =\> false do |t|  
t.integer :user\_id  
t.integer :request\_id  
end

Now you can assign as many users for as many requests as you want and  
vice versa.

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [April 23, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/associations-in-rails/166280/3 "2009-04-23T19:53:30Z")

</div>

Thanks Heinz for the reply

I fixed the problem by using

User Model:

has\_many :requests  
has\_many :requesttargetusers  
has\_many :targetrequests,:through=\>  
:requesttargetusers,:source=\>:request

Request Model

belongs\_to :user  
has\_many :requesttargetusers  
has\_many :targetusers, :through=\> :requesttargetusers, :source=\>:user

Requesttargetuser model:

belongs\_to :users  
belongs\_to :requests

And this worked for me…

Heinz S. wrote:

> I don’t know if I got your question right but what if you just:
> 
> User model:  
> has\_and\_belongs\_to\_many :requests
> 
> Request model:  
> has\_and\_belongs\_to\_many :users
> 
> and create a habtm table like:  
> create\_table :users\_requests, :id =\> false do |t|  
> t.integer :user\_id  
> t.integer :request\_id  
> end
> 
> Now you can assign as many users for as many requests as you want and  
> vice versa.
