# Many to Many Question

**URL:** <https://www.ruby-forum.com/t/many-to-many-question/151403>\
**Category:** Rails\
**Created:** [October 21, 2008, 2:13am UTC](https://www.ruby-forum.com/t/many-to-many-question/151403 "2008-10-21T02:13:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [October 21, 2008, 2:13am UTC](https://www.ruby-forum.com/t/many-to-many-question/151403/1 "2008-10-21T02:13:25Z")

</div>

Hello,

I have two many-to-many relationships modeled like so:

- a Company has many Users
- a User has many Bugs

The Company model has:

has\_many :users

The User model has:

belongs\_to :company  
has\_many :bugs

The Bug model has:

belongs\_to :user

Should I be able to say:

\<%= @company.users.bugs.count %\>

(Currently, I’m getting the error “undefined method ‘bugs’ for …”  
trying to do this.)

Or do I have to tack a ‘company\_id’ onto each bug and say  
@company.bugs.count?

Thanks,

-scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [October 21, 2008, 2:46am UTC](https://www.ruby-forum.com/t/many-to-many-question/151403/2 "2008-10-21T02:46:46Z")

</div>

Scott,

I think you would have to:

-Find all users belonging to a company (@company.users.find(:all))  
-Iterate through each user and add their bug count to a total

bug\_count = 0  
@company.users.find(:all).each do |user|  
bug\_count = bug\_count + user.bugs.size  
end

If you have a large company or lots of users you may be better off  
writing a custom SQL statement and letting your database do the  
counting.

Evan

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [October 21, 2008, 5:47am UTC](https://www.ruby-forum.com/t/many-to-many-question/151403/3 "2008-10-21T05:47:33Z")

</div>

On Oct 21, 11:12 am, Scott [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= @company.users.bugs.count %\>

@company.users is just an array of User objects. Similarly  
@user.bugs.

> (Currently, I’m getting the error “undefined method ‘bugs’ for …”  
> trying to do this.)

You could probably do something like this in your company class:  
has\_many :bugs , :through =\> :users  
So @company.bugs.size.

Someone else mentioned:  
bugs=0  
@company.users.each do |user|  
bugs+=user.bugs.size  
end  
With regards iterating @company.users and all the sql being generated,  
you might be able to do some eager loading using ‘:include’ when you  
find @company. eg :include =\> { ‘users’ =\> ‘bugs’ }  
Company.find(:first,:include =\> { ‘users’ =\> ‘bugs’})  
This creates a wall of sql with left outer joins between the tables.  
No doubt there are ways to tweak and adjust all of this. If you only  
need to count, then getting the db to count might be better (as  
mentioned) or an sql ‘group by’ to do a summation (AR provides options  
for this).

Compare the sql in your logs between ‘has\_many :through’ vs the  
iteration/eager-loading. The former may not rely on left outer joins  
and seems neater.

–  
Daniel B.
