I want to display all the projects in which a given team has a product. The following code works, but isn''t there a better way? Railsier? Better Practice? #Models# class Team < ActiveRecord::Base has_many :products has_many :funding_sources, :through=>:products end class Product < ActiveRecord::Base belongs_to :funding_source belongs_to :team end class FundingSource < ActiveRecord::Base belongs_to :project has_many :products end class Project < ActiveRecord::Base has_many :funding_sources has_many :products, :through=>:funding_sources has_many :teams, :through=>:products #Can I do this? end #projects_controller.rb# # Goal: all the projects in which a team has a product. # GET /projects/by_team/team_id # GET /projects/by_team/team_id.xml/ def by_team begin @team = Team.find(params[:team_id]) rescue @projects=Project.find(:all) else products=@team.products funding_ids = products.reduce([]){|items, product| items.push(product.funding_source_id)} fundings=FundingSource.find(:all, :select=>:project_id,:conditions=>["id IN (?)", funding_ids.uniq]) project_ids = fundings.reduce([]){|items, funding| items.push(funding.project_id)} @projects = Project.find(:all, :conditions=>["id IN (?)", project_ids.uniq]) end respond_to do |format| format.html #by_team.html.erb format.xml { render :xml => @projects } end end I can add some of the -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Well, this is a little better. . . . else fundings=@team.funding_sources project_ids = fundings.reduce([]){|items, funding| items.push(funding.project_id)} @projects = Project.find(:all, :conditions=>["id IN (?)", project_ids.uniq]) end . . . So, now the problem becomes: how do I get all the projects that belong to a bunch of funding sources? I am just sure there''s a syntax that I missed in the Rails Associations tut. "fundings.projects" doesn''t work. Ron -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Added this to Project model: def self.find_in_funding_sources(f_s) project_ids = f_s.reduce([]){|items, funding| items.push(funding.project_id)} projects = Project.find(:all, :conditions=>["id IN (?)", project_ids.uniq]) return projects end now the controller looks better: . . . else @projects Project.find_in_funding_sources(@team.funding_sources) end . . . I don''t know why I thought associations would take care of this. Anyway, if they will, let me know how, please. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.