# Filtered display

**URL:** <https://www.ruby-forum.com/t/filtered-display/154253>\
**Category:** Rails\
**Created:** [November 26, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/filtered-display/154253 "2008-11-26T21:15:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [November 26, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/filtered-display/154253/1 "2008-11-26T21:15:49Z")

</div>

Hello all. I’ve set up an inventory app and I want to be able to have  
the index file display ether everything in inventory or only items on  
order. So I want to populate @spares with the appropriate content then  
pass it to index.html.erb  
Is this the right approach? If so how do put logic into the controller  
to populate @spares ?  
Any help appreciated!  
…Bill

------------spares\_controller.rb  
def index  
@spares = Spare.find(:all) \<–do this to see total inventory  
@spares = Spare.find(:all, :conditions =\> [“state = ?”, “On Order”])  
\<–do this to see what’s on order  
…  
end

-------------index.html.erb  
\<% for spare in @spares %\>

 \<%=h spare.name %\> \<%=h spare.partno %\> \<%=h spare.state %\> ... \<% end %\>

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [November 27, 2008, 3:11am UTC](https://www.ruby-forum.com/t/filtered-display/154253/2 "2008-11-27T03:11:58Z")

</div>

Bill McGuire wrote:

> Hello all. I’ve set up an inventory app and I want to be able to have  
> the index file display ether everything in inventory or only items on  
> order. So I want to populate @spares with the appropriate content then  
> pass it to index.html.erb  
> Is this the right approach? If so how do put logic into the controller  
> to populate @spares ?  
> Any help appreciated!  
> …Bill

It looks like you’re generally going in the right direction. However,  
you’re setting @spares twice, so your view will only see the results of  
the second #find. It’s not really clear to me what effect you’re going  
for here.

I also recommend using named\_scope for the “on order” items, if you’re  
using Rails 2.1 or above. They’re a bad-ass way to encapsulate common  
filters on your objects.

[http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality](http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality)

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [November 27, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/filtered-display/154253/3 "2008-11-27T19:24:18Z")

</div>

Jeremy Weiskotten wrote:

> Bill McGuire wrote:
> 
> > Hello all. I’ve set up an inventory app and I want to be able to have  
> > the index file display ether everything in inventory or only items on  
> > order. So I want to populate @spares with the appropriate content then  
> > pass it to index.html.erb  
> > Is this the right approach? If so how do put logic into the controller  
> > to populate @spares ?  
> > Any help appreciated!  
> > …Bill
> 
> It looks like you’re generally going in the right direction. However,  
> you’re setting @spares twice, so your view will only see the results of  
> the second #find. It’s not really clear to me what effect you’re going  
> for here.
> 
> I also recommend using named\_scope for the “on order” items, if you’re  
> using Rails 2.1 or above. They’re a bad-ass way to encapsulate common  
> filters on your objects.
> 
> [http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality](http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality)

Thanks for the reply Jeremy. I didn’t mean to show that I was populating  
@spares twice, I was looking for a way to populate it one of the two  
ways depending on what the user wanted to see.  
I’ll take a look at the link you sent, might be the way to go.  
Thanks again.
