DaveG
DaveG

Reputation: 1203

Sunspot/Solr Rails Filter

UPDATED

I'm using sunspot/solr to search and filter data in my Rails 3.2 application. This is the second of two solr searches I'm performing on the "Gear" model.

My first search displays all of the gears in the view based on the query and the facets applied (A gear belongs to a user, and a user has many gears).

Now I'm probably thinking about this incorrectly (because I haven't looked at this in awhile).

Basically I'm now building a new search but this is also on the "Gear" model but it's in a different controller, different view and I'm trying to only search/filter/facet the results of the Users gears but I'm getting ALL of the gears in the database.

Getting following error...

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

What am I missing?

Show Action in Controller

   def show
     @store = Store.find(params[:id])
     @user = @store.user
     @search2 = Gear.solr_search do
          fulltext params[:search]        
          facet (:category_name)
          facet(:sub_category_name)
          facet (:state)
          facet (:city)
          facet (:price)
          with(:user_id, @user.id)
          with(:price, params[:price]) if params[:price].present?
          with(:state, params[:state]) if params[:state].present?
          with(:city, params[:city]) if params[:city].present?
          with(:sub_category_name, params[:name]) if params[:name].present?
          with(:category_name, params[:categoryname]) if params[:categoryname].present?
          paginate(page: params[:page], :per_page => 15)
     end

     @gears = @search2.results
  end

I was doing this @gears = @store.user.gears.paginate(page: params[:page]) before trying sunspot.

Gear Model

class Gear < ActiveRecord::Base
  attr_accessible :title, :size, :price, :sub_category_id, :user_id, :image, :image_a, :image_b, :image_c, :image_d, :image_e, :image_f, :image_g, :image_h, :image_i, :remote_image_url, :color, :year, :latefee, :cancellation, :minrental, :policy, :about, :address, :city, :state, :zip, :country, :latitude, :longitude, :gmaps
  belongs_to :user
  belongs_to :sub_category

  searchable do
     text :title, :size, :price, :year, :zip, :state, :city, :minrental, :about, :latefee, :color

     text :user_firstname do
          user.firstname
     end

     text :user_lastname do
          user.lastname
     end
     # **Facet Section**   

     string :size 
     string :price
     string :state
     string :city

     string :sub_category_name , :multiple => true, :stored => true do
       sub_category.name
     end

     string :category_name, :multiple => true, :stored => true do
       category.name
     end
   end
end

Store Model

class Store < ActiveRecord::Base
  attr_accessible :storeimage, :storename
  belongs_to :user
  mount_uploader :storeimage, StoreUploader
end

User Model

class User < ActiveRecord::Base 

  attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :userimage, :remove_userimage, :name
  has_many :gears
  has_many :comments, :dependent => :destroy 
  has_one :store, :dependent => :destroy
  require 'carrierwave/orm/activerecord'
  mount_uploader :userimage, UserpicUploader
  accepts_nested_attributes_for :store


end

Upvotes: 0

Views: 1119

Answers (1)

kulesa
kulesa

Reputation: 2964

UPDATED

Thanks for publishing models code. Please check if this works for you now. Note user = @store.user assignment in controller action uses local user variable rather than instance variable @user. Don't forget to reindex your models after adding :user_ids to searchable fields.

class Gear < ActiveRecord::Base
  searchable do
    integer :user_id
  end
end

# now the controller action
def show
  @store = Store.find(params[:id])
  user = @store.user

  @search2 = Gear.solr_search do
    with(:user_id, user.id)
    # all other search code omitted for brevity
  end
end

Upvotes: 1

Related Questions