# Can a model have both a has\_many and has\_many through?

**URL:** <https://www.ruby-forum.com/t/can-a-model-have-both-a-has-many-and-has-many-through/186903>\
**Category:** Rails\
**Created:** [April 14, 2010, 2:37am UTC](https://www.ruby-forum.com/t/can-a-model-have-both-a-has-many-and-has-many-through/186903 "2010-04-14T02:37:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 14, 2010, 2:37am UTC](https://www.ruby-forum.com/t/can-a-model-have-both-a-has-many-and-has-many-through/186903/1 "2010-04-14T02:37:42Z")

</div>

I’ll try to explain this as best as I can… I have an Assignment and  
User model, with users having many assignments and the assignment  
belonging to a user. But, along with assignments having one owner, I’d  
like for assignments to have multiple “secondary” owners, through the  
use of “has\_many :through =\> assignments\_users”. Is this possible? I  
tried to implement this but I got an error saying an assignment with  
the user id of 1 can’t be found, when there were assignments with the  
user\_id as 1, but it was looking through the assignments\_users table.  
Thanks!

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 15, 2010, 6:41pm UTC](https://www.ruby-forum.com/t/can-a-model-have-both-a-has-many-and-has-many-through/186903/2 "2010-04-15T18:41:19Z")

</div>

Andrew wrote:

> I’ll try to explain this as best as I can… I have an Assignment and  
> User model, with users having many assignments and the assignment  
> belonging to a user. But, along with assignments having one owner, I’d  
> like for assignments to have multiple “secondary” owners, through the  
> use of “has\_many :through =\> assignments\_users”. Is this possible? I  
> tried to implement this but I got an error saying an assignment with  
> the user id of 1 can’t be found, when there were assignments with the  
> user\_id as 1, but it was looking through the assignments\_users table.  
> Thanks!

User  
has\_many :assignments\_users  
has\_many :assignments, :through =\> :assignments\_users  
end

Assignment  
has\_many :assignments\_users  
has\_many :users, :through =\> :assignments\_users  
belong\_to :owner, :class =\> User, :foreign\_key =\> :owner\_id  
end

AssignmentUser  
belongs\_to :assignment  
belongs\_to :user  
end

Would provide these message:  
@user.assignments  
@assignment.users  
@assignment.owner

However, I would likely alter the naming of these objects somewhat.

Something like:

User  
has\_many :assignments  
has\_many :projects, :through =\> :assignments  
end

Project  
has\_many :assignments  
has\_many :users, :through =\> :assignments  
belong\_to :owner, :class =\> User, :foreign\_key =\> :owner\_id  
end

Assignment  
belongs\_to :project  
belongs\_to :user  
end

In this case User and Project are solidly nouns. Assignment (although  
could be a noun) servers better for linking Users to Projects. However,  
this is really just semantics, although I do make an effort to give my  
joining models a good solid name when using has\_many :through.

I like this because the act of assigning a project to a user can be more  
easily thought of from a RESTful perspective. In order to “create” an  
association between a user and a project, one would create an  
assignment.

## assignments\_controller

def create  
user # assume this exists  
project # assume this exists  
@assignment = Assignment.new  
@assignment.user = user  
@assignment.project = project  
end

Note: The above code is intended for demonstration purposes only. This  
could be re-factored to reduce the complexity of the controller code.
