1

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?

Community
  • 1
  • 1
BigBoy1337
  • 4,735
  • 16
  • 70
  • 138
  • 1
    can you improve formatting of the question you have asked. Also your question is not clear. Do you want to search for sale and purchase through same contoller? – Paritosh Singh Sep 25 '12 at 20:49
  • i hope my edits helped. please let me know if they dont – BigBoy1337 Sep 25 '12 at 21:24
  • What attributes of your model do you want to look into for a match with your search? – Anthony Alberto Sep 25 '12 at 23:09
  • I just want to search the content of the microposts. content is another column of the micropost table. so if they search bike on the sale side for example and there are 4 microposts with the sale attribute and bike in the content string, then the sale list shows only those 4 microposts – BigBoy1337 Sep 25 '12 at 23:52

3 Answers3

1

I would start with the model

class Micropost < ActiveRecord::Base

  ...
  scope :purchases, where(:kind => 'purchase')
  scope :sales, where(:kind => 'sale')
  ...

Now you can use Micropost.purchases and Micropost.sales and add more .where as needed for the search criteria, e.g.

@sales = Micropost.sales.where('name like ?',params[:name])
@purchases= Micropost.purchases.where('name like ?',params[:name])

In your views you'll have two lists to display @purchases and @sales, something like:

<%-    @purchases.each do |one_purchase} %>
<%=      one_purchase.name %>
%br
<%-    end %>

<%-    @sales.each do |one_purchase} %>
<%=      one_purchase.name %>
%br
<%-    end %>
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • can you expand on your answer? It seems you are suggesting that I use the .where as a way to search. This is inconsistent with what I have read (or at least not mentioned) in http://railscasts.com/episodes/37-simple-search-form. Is this what you intended? – BigBoy1337 Sep 26 '12 at 09:31
  • Updated with view and controller info – Michael Durrant Sep 26 '12 at 11:23
  • your view seems like it is listing all of the purchases and sales individually. I still don't understand how this accomplishes the searching that I described, and how I can differentiate between the two kinds of search – BigBoy1337 Sep 26 '12 at 20:13
0

If you want to search through tables, I would recommend DataTables, a jQuery plugin. It will allow you to do what you want. Plus, it will do dynamic filtering.

TheDude
  • 3,796
  • 2
  • 28
  • 51
0

You should read this guide on the ActiveRecord Query Interface, but here's the answer to your question:

@purchases = Micropost.where(['`microposts`.kind = ?', 'purchase'])
@sales = Micropost.where(['`microposts`.kind = ?', 'sale'])

You could also set up a scope.

Regarding search, I would recommend ThinkingSphinx as a simple, light-weight solution:

You could set up your micropost scopes and indices like so:

class Micropost < ActiveRecord::Base

  # ...

  scope :purchases, where(:kind => 'purchase')
  scope :sales, where(:kind => 'sale')

  # Here you can define which columns are searchable.

  define_index do
    indexes :name
  end

  # You can even search based on associations, but this is even more complicated
  # and outside the scope of this question. My point: It's very adapatable..

  # Using a sphinx_scope..
  sphinx_scope(:by_kind) { |kind| { :conditions => { :kind => kind } } }

end

Using the sphinx engine is simple through ThinkingSphinx. In your controller, for example:

def search
  @microposts = Micropost.search(params[:query]).compact

  respond_to do |format|
    format.html
  end
end

# Or, using the sphinx_scope:
# Assumes params[:kind] contains either 'sale' or 'purchase'
def search_by_kind
  @microposts = Micropost.by_kind(params[:kind]).search(params[:query]).compact

  respond_to do |format|
    format.html
  end
end

In your form..

<select id="select_kind" name="kind">
  <option value="purchase" selected="selected">Purchase</option>
  <option value="sale">Sale</option>
</select>

.. which will translate into params[:kind]..

Adam Eberlin
  • 14,005
  • 5
  • 37
  • 49
  • A scope is a good idea. That is what I decided to do. Any idea on the answer to my question about searching? – BigBoy1337 Sep 26 '12 at 20:42
  • See my updates. My general suggestion is to try out ThinkingSphinx, which is a simple, lightweight solution with great documentation for installation. If deployment ever becomes an issue, it's easily manageable by capistrano and rake. – Adam Eberlin Sep 27 '12 at 16:55
  • Using ThinkingSphinx seems to be really promising. I am still unclear on how to format the two search forms. How can I differentiate the "purchases" search form from the "sales" search form? – BigBoy1337 Sep 28 '12 at 00:35