# Users HM Projects, Projects HM Activities: Question on find

**URL:** <https://www.ruby-forum.com/t/users-hm-projects-projects-hm-activities-question-on-find/129604>\
**Category:** Rails\
**Created:** [February 20, 2008, 1:08am UTC](https://www.ruby-forum.com/t/users-hm-projects-projects-hm-activities-question-on-find/129604 "2008-02-20T01:08:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [February 20, 2008, 1:08am UTC](https://www.ruby-forum.com/t/users-hm-projects-projects-hm-activities-question-on-find/129604/1 "2008-02-20T01:08:18Z")

</div>

As the topic suggests, I have a USER model that has many Projects, and a  
PROJECT model that has many Activities. What is the easiest way to find  
all Activities that belong to the logged in user? Is this something  
that isn’t possible and should I also create a user\_id in activities and  
create a BT/HM relationship there?

Thank you

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [February 20, 2008, 1:38am UTC](https://www.ruby-forum.com/t/users-hm-projects-projects-hm-activities-question-on-find/129604/2 "2008-02-20T01:38:11Z")

</div>

> As the topic suggests, I have a USER model that has many Projects, and a  
> PROJECT model that has many Activities. What is the easiest way to find  
> all Activities that belong to the logged in user? Is this something  
> that isn’t possible and should I also create a user\_id in activities and  
> create a BT/HM relationship there?

Read about has many through…

If you end up with lots of users/projects though it won’t be really  
really  
fast. If you want it lightning fast add user\_id to activities so you  
can  
query it directly.

class Assignment \< ActiveRecord::Base  
belongs\_to :programmer # foreign key - programmer\_id  
belongs\_to :project # foreign key - project\_id  
end  
class Programmer \< ActiveRecord::Base  
has\_many :assignments  
has\_many :projects, :through =\> :assignments  
end  
class Project \< ActiveRecord::Base  
has\_many :assignments  
has\_many :programmers, :through =\> :assignments  
end

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [February 20, 2008, 3:10am UTC](https://www.ruby-forum.com/t/users-hm-projects-projects-hm-activities-question-on-find/129604/3 "2008-02-20T03:10:56Z")

</div>

Philip H. wrote:

> > As the topic suggests, I have a USER model that has many Projects, and a  
> > PROJECT model that has many Activities. What is the easiest way to find  
> > all Activities that belong to the logged in user? Is this something  
> > that isn’t possible and should I also create a user\_id in activities and  
> > create a BT/HM relationship there?
> 
> Read about has many through…
> 
> If you end up with lots of users/projects though it won’t be really  
> really  
> fast. If you want it lightning fast add user\_id to activities so you  
> can  
> query it directly.
> 
> class Assignment \< ActiveRecord::Base  
> belongs\_to :programmer # foreign key - programmer\_id  
> belongs\_to :project # foreign key - project\_id  
> end  
> class Programmer \< ActiveRecord::Base  
> has\_many :assignments  
> has\_many :projects, :through =\> :assignments  
> end  
> class Project \< ActiveRecord::Base  
> has\_many :assignments  
> has\_many :programmers, :through =\> :assignments  
> end

Thanks. For what it’s worth, I just solved my own problem using a  
little bit or ruby:

```
@activity_list = []
projects = Project.find(:all, 

```

:conditions=\>[‘user\_id=?’,current\_user.id])  
projects.each do |p|  
p.activities.each do |a|  
@activity\_list \<\< a if a.completed == nil && a.win\_at != nil  
end  
end

```
@activities = @activity_list.sort_by { |a| a.win_at }
```
