-
-
Save mguterl/1392438 to your computer and use it in GitHub Desktop.
Sunspot: How to filter?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BusinessStore < ActiveRecord::Base | |
attr_accessible :website, :name, :address, :phone_number, :online_store | |
has_many :products | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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