ruby on rails 3 - How to use the search in acts as taggable -
i using acts-as-taggable-on gem.
i want use search both name tags in single search field
my model
class user < activerecord::base acts_as_taggable attr_accessor: :name, :age, :country, tag_list def self.search(search) if search where('name ?', "%#{search}%") else scoped end end end
controller
class userappscontroller < applicationcontroller def index @users = user.search(params[:search]) //@users = user.tagged_with(params[:search]) end end
help me solve this.
one way add both results 1 using + :
@users = user.search(params[:search]) @users = @users + user.tagged_with(params[:search])
a better solution change search method in model return full result:
where('name ?', "%#{search}%") + tagged_with(search)
but if want split name search tag search, can split them scopes or , use it:
scope :name_contains, -> (query) { where('name ?', "%#{query}%") }
and in search method:
name_contains(search) + tagged_with(search)
btw if want name search case insensitive use like:
scope :name_contains, -> (query) { where("replace (lower(name), ' ', '' ) ?", "%#{query.downcase}%") }
Comments
Post a Comment