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

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

Philip Hallstrom 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 }