0

I have a materials model which has a search form. The search action looks a bit like this:

def search
  conditions = {}
  conditions[:version] = 'master'
  conditions[:status] = 'shared'
  conditions[:targ_lang] = params[:targ_lang] unless params[:targ_lang].blank?
  @results = Material.find(:all, :conditions => conditions)
end

I have added the acts-as-taggable gem and it works fine to save the tags but I'm having trouble adding it to the search form. The documentation states that to find Materials with the tags you can use this code:

Material.tagged_with(["awesome", "cool"], :match_all => true)

But I don't know how to add this condition to the conditions.

Update

@results = Material.where(conditions) && Material.tagged_with(params[:tag_list])

This works provided tags are used but it doesn't work if the tag list is blank so I need a condition as with the other conditions above that the Material.tagged_with ... part is only necessary if the field is not empty.

Update 2 - Bad Solution

This works but it's not very elegant is it?

if params[:tag_list].blank?
  @results = Material.where(conditions)
else 
  @results = Material.tagged_with(params[:tag_list]).where(conditions)
end
Finnjon
  • 651
  • 4
  • 19

1 Answers1

0

This code won't work for you?

Material.where(conditions).tagged_with(['awesome', 'cool'], :match_all => true)

Or the inverse order:

Material.tagged_with(['awesome', 'cool'], :match_all => true).where(conditions)

UPDATE

Reading the docs on the act-as-taggable-one on github, there is a option named :any. Maybe you can try to use it. I don't have a project i could do some testing, but maybe a code like:

Material.tagged_with(['awesome', 'cool', '', nil], :any => true).where(conditions)

Give it a try.

MurifoX
  • 14,991
  • 3
  • 36
  • 60
  • This works but the problem is that it does not take into account an empty field for the tags. So if there are no tags there are no results. What is needed is the tags to be just another condition. – Finnjon Sep 17 '13 at 13:18
  • Hmm, i understood your problem now. I just updated the answer with some guesses. Maybe it could point you to the right direction. – MurifoX Sep 17 '13 at 13:47