ruby - Search functionality rails 4 -


i have current implementation allows me filter search results category name

class category < activerecord::base   has_many :bike_categories   has_many :bikes, through: :bike_categories end  class bikecategory < activerecord::base    # holds bike_id , category_id allow multiple categories saved per image, opposed storing array of objects in 1 db column   belongs_to :bike   belongs_to :category end  class bike < activerecord::base   has_many :bike_categories, dependent: :destroy   has_many :categories, through: :bike_categories end  model  def self.search(params)   includes_categories(params[:name]) end  def self.includes_categories(category_names)   joins(:categories)   .where(categories: { name: category_names })   .group('bikes.id')   .having("count(*) = ?", category_names.size) end 

so if have following data

bike id: 1 title: 'bike 1' id: 2 title: 'bike 2'  category id: 1 name: 'mens' id: 2 name: 'womens' id: 3 name: 'mountain bike' id: 4 name: 'hybrid'  bike_categories id: 1 bike_id: 1 :category_id: 2 # womens, mountain bike id: 2 bike_id: 1 :category_id: 3   id: 3 bike_id: 2 :category_id: 2 # womens, hybrid id: 4 bike_id: 2 :category_id: 4 

in filter if choose womens , mountain bikes bikes categories womens, mountain bikes, in example 1 result.

however go 1 step further , select category, hybrid (so select filters womens, mountain bikes, hybrid) , bikes have either womens, mountain bikes , womens, hybrid, in instance should 2 results returned

how modify query allow ?

thanks

i have used along following lines allow filtering of search results multiple attributes

# style mountian bike etc. class style < activerecord::base   has_many :bike_styles   has_many :bikes, through: :bike_styles end  class bikestyle < activerecord::base   belongs_to :bike   belongs_to :style end  # gender womens, mens etc.  # (or maybe there better name if have girls / boys well) class gender < activerecord::base   has_many :bike_genders   has_many :bikes, through: :bike_genders end  class bikegender < activerecord::base   belongs_to :bike   belongs_to :gender end  class bike < activerecord::base   has_many :bike_styles, dependent: :destroy   has_many :styles, through: :bike_styles    has_many :bike_genders, dependent: :destroy   has_many :genders, through: :bike_genders    # repeat other searchable attributes ( e.g. frame size )   # has_many :bike_sizes, dependent: :destroy   # has_many :sizes, through: :bike_sizes    # returns filtered results based on params, call follows:   #   bikes.search style: "mountian bike", gender: "mens"       #   bikes.search style: "mountian bike"   #   bikes.search gender: "mens"   def self.search(params)     filtered = params[:collection] || self     filtered = style_filter(filtered, params[:style])     filtered = gender_filter(filtered, params[:gender])     filtered   end    # filters style (if 1 provided)     def self.style_filter(filtered, style)      filtered = filtered.joins(:styles).where(styles: {name: style}) unless style.blank?      filtered    end    # filters gender (if 1 provided)     def self.gender_filter(filtered, gender)      filtered = filtered.joins(:genders).where(genders: {name: gender}) unless gender.blank?      filtered    end end 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -