Robert Malko
2008-Feb-20 00:08 UTC
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? Thank you -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2008-Feb-20 00:26 UTC
Re: 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> > Thank you > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Malko
2008-Feb-20 02:10 UTC
Re: Users HM Projects, Projects HM Activities: Question on f
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 > endThanks. 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 } -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---