Skip to content

Instantly share code, notes, and snippets.

@mguterl
Forked from Justinwceo/BusinessStore.rb
Created November 24, 2011 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mguterl/1392438 to your computer and use it in GitHub Desktop.
Save mguterl/1392438 to your computer and use it in GitHub Desktop.
Sunspot: How to filter?
class BusinessStore < ActiveRecord::Base
attr_accessible :website, :name, :address, :phone_number, :online_store
has_many :products
end
class CreateBusinessStores < ActiveRecord::Migration
def self.up
create_table :business_stores do |t|
t.string :name
t.string :address
t.string :website
t.string :phone_number
t.boolean :online_store
t.timestamps
end
end
end
class Product < ActiveRecord::Base
attr_accessible :price, :name, :purchase_date, :store
belongs_to :business_store
attr_accessor :store # for finding particular ID of business_store
searchable do
text :name
time :purchase_date
float :price
boolean :online_store
end
def online_store
# I'm assuming all products have a business_store otherwise you
# might want this business_store.try(:online_store) or take a look
# at the delegate method that's included in ActiveRecord::Base.
business_store.online_store
end
end
class SearchController < ApplicationController
def index
# Pass a block variable or else you won't be able to access the
# params here.
@search = Product.search do |q|
q.fulltext params[:search]
q.with(:online_store, params[:online_store] == 1)
q.paginate(:per_page => 10, :page => params[:page])
q.order_by(:purchase_date, :desc)
q.order_by(:price,:asc)
end
@products = @search.results
end
end
<%= form_tag search_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<%end%>
<% @products.each do |p| %>
<%= p.name %>
<%= p.price %>
<%= p.purchase_date. %>
<%= p.store %>
<% if p.business_store.online_store %>
<%= "Online Store" if p.business_store.online_store %>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment