Ransack and acts-as-taggable-on issues
Asked Answered
B

1

7

I`ve got a model with several taxonomies using acts-as-taggable-on

class Film < ActiveRecord::Base
  attr_accessible :mood_list, :genre_list, :tag_list, :country_list
  acts_as_taggable_on :countries, :genres, :moods, :tags
end

I`m trying to make Ransack search find records that have any of moods, and exactly matching country. But it retuns 0 records whereas i have a matching record, see:

Film.search({:moods_name_in=>['happy', 'snooze']}).result.count => 2    
Film.search({:countries_name_eq=>'USA'}).result.count => 2
Film.search({:moods_name_in=>['happy', 'snooze'], :countries_name_eq=>'USA'}).result.count => 0

It returns nothing!!! But i definitely have one:

>> f.countries
[#<ActsAsTaggableOn::Tag id: 13, name: "USA">]
>> f.moods
[#<ActsAsTaggableOn::Tag id: 17, name: "active">, #<ActsAsTaggableOn::Tag id: 16, name: "psyched">, #<ActsAsTaggableOn::Tag id: 15, name: "happy">]
The SQL generated is:
(3.2ms)  SELECT COUNT(DISTINCT "films"."id") FROM "films" LEFT OUTER JOIN "taggings" ON "taggings"."taggable_id" = "films"."id" AND taggings.context = ('moods') AND "taggings"."taggable_type" = 'Film' LEFT OUTER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" LEFT OUTER JOIN "taggings" "country_taggings_films_join" ON "country_taggings_films_join"."taggable_id" = "films"."id" AND taggings.context = ('countries') AND "country_taggings_films_join"."taggable_type" = 'Film' LEFT OUTER JOIN "tags" "countries_films" ON "countries_films"."id" = "country_taggings_films_join"."tag_id" WHERE (("tags"."name" IN ('happy', 'snooze') AND "countries_films"."name" = 'USA'))

any help please!

UPDATE: 1/11/2013 The issue should lie in correct applying ransack predicates. There are several related questions which actually are not solved. Rails, Ransack: How to search HABTM relationship for "all" matches instead of "any"

Convert ActiveRecord habtm query to Arel

UPDATE It looks like the issue is unsolvable event for has_and_belongs_to_many https://github.com/ernie/ransack/issues/164

Barnwell answered 17/10, 2013 at 8:18 Comment(0)
L
1

You can add it by association, f.e.: class Person < ActiveRecord::Base PERSON_SEARCH_OPTIONS = :name_or_email_or_tags_name_cont end It works for me

I think it is the better way to do it.

Lesleelesley answered 11/10, 2016 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.