# This works, but it can't be best practice. Advice?

**URL:** <https://www.ruby-forum.com/t/this-works-but-it-cant-be-best-practice-advice/187309>\
**Category:** Rails\
**Created:** [April 21, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/this-works-but-it-cant-be-best-practice-advice/187309 "2010-04-21T14:46:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@paron](https://www.ruby-forum.com/u/paron)\
**Post date:** [April 21, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/this-works-but-it-cant-be-best-practice-advice/187309/1 "2010-04-21T14:46:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@paron](https://www.ruby-forum.com/u/paron)\
**Post date:** [April 21, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/this-works-but-it-cant-be-best-practice-advice/187309/2 "2010-04-21T16:06:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@paron](https://www.ruby-forum.com/u/paron)\
**Post date:** [April 21, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/this-works-but-it-cant-be-best-practice-advice/187309/3 "2010-04-21T17:19:07Z")

</div>

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.
