0

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?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Frexuz
  • 4,732
  • 2
  • 37
  • 54
  • No it wont work. That isnt how acts_as_taggable was written to work. When you submit a taglist it generates a list of all tags from your query string and uses it to fetch tagged records. It doesnt make a search on the list of tags to find the tags that match your query. – Steve Robinson Jul 06 '13 at 10:32

2 Answers2

1

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.

Frexuz
  • 4,732
  • 2
  • 37
  • 54
1
@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