Search for models tagged_with OR title like (with actions_as_taggable_on)

I am performing a model search using a region. This is accessed by the search form with the search parameter q. I currently have the code below that is great for searching tags related to the model. But I would also like to find the title field. If I add to this area, then I will get all the results where there is a tag and title that match the search query.

However, I need to return results matching the company_id and category_id files, and / or matching headers or tags. I am stuck on how to add an OR clause to this area.

  def self.get_all_products(company, category = nil, subcategory = nil, q = nil)
    scope = scoped{}
    scope = scope.where "company_id = ?", company
    scope = scope.where "category_id = ?", category unless category.blank?
    scope = scope.tagged_with(q) unless q.blank?
    scope
  end

I am using Rails 3.

+3
source share
2 answers

- ? -

t = TableName.arel_table
scope = scope.where(t[:title].eq("BLAH BLAH").or(t[:tag].eq("blah blah")))

scope = scope.where("title = ? OR tag = ", title_value, tag_value)
+3

, , or. , where. , - ...

clause = "company_id=?"
qparams = [company]

unless category.blank?
    clause += " or category_id=?"
    qparams <= category
end

scope.where clause, *qparams
0

Source: https://habr.com/ru/post/1793503/


All Articles