Associations In rails


#1

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


#2

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.


#3

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.