Вопрос

So I'm using acts_as_taggable on a model. I'd like to be able to find tags with some sort of a %LIKE% matching, but I'm not sure how.

My current code:

@companies = Company.tagged_with(@query, :any => true)

doing this doesn't work:

tagged_with("%#{@query}%", :any => true)

Any ideas?

Это было полезно?

Решение

Solved by fetching the tags manually first

  tags = Tag.where("name LIKE ?", "%#{@query}%").pluck(:name)
  @companies = Company.tagged_with(tags, :any => true)

However, this required my to create an empty Tag model, which isn't created by acts_as_taggable.

Maybe not the best solution, but it works :)
I'll rather do this "hack", then to write my own tagging models.

Другие советы

@companies = Company.tagged_with('query', wild: true)

You should try it.

You can also use :wild => true option along with :any or :exclude option. It will be looking for %awesome% and %cool% in SQL

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top