right now I have an object(i think) called Microposts. Its a table in the database that has a column named kind. This can either be "purchase" or "sale". On the home page I have a list of every users microposts seperated into two lists. One has only sale microposts and the other only purchase microposts. I am trying to put a search bar on top of each of these lists that can search through only those. Then when the user searches, the column displays the search results.
The problem is that all of the tutorials I find such as http://railscasts.com/episodes/37-simple-search-form?autoplay=true go through how to search through an object with its own controller (such as if I wanted to search through microposts). How can I only search through microposts that are purchase. or only search through those that are sale?
Edit: Clarification
Here is what I am supposed to add in the app/views/home.html.erb
<% form_tag microposts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
but what I would like is two of these, one that searches through all microposts that have the kind (kind is a column in the micropost table) purchase and one that searches through all microposts that have the kind sale. I don't know the correct syntax for this.
Also, here is the relevant part of my microposts_controller.rb
def home
@microposts=Micropost.all
@purchases=@microposts.collect{ |m| m if m.kind == "purchase"}.compact
@sales=@microposts.collect{ |m| m if m.kind == "sale"}.compact
end
Edit 2:
you all have good suggestions for defining the @microposts, @purchases, and @sales. I think using scopes are the way to go. However, I still fail to see how this answers this particular question. How can I differentiate these two search forms, and search definitions so that one searches through all the microposts with the kind sale and one through all the microposts with the kind purchase. I want the searches to be completely different. Can I id the search forms somehow? How can I define the sale search and purchase search differently in the model. I know I am supposed to have something like
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
in micropost.rb. Do I need one definition for purchases and one for sales?
Edit again:
I have tried to make this question more clear at RoR: how can I search only microposts with a certain attribute?