悲剧和行为
我已经有了一个使用行为作为标签的几个分类法的模型
class Film < ActiveRecord::Base
attr_accessible :mood_list, :genre_list, :tag_list, :country_list
acts_as_taggable_on :countries, :genres, :moods, :tags
end
我试图让Ransack搜索找到有任何情绪的记录,并且完全匹配国家。 但它会回退0条记录,而我有匹配的记录,请参阅:
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
它什么都不返回! 但我绝对有一个:
>> 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'))
请任何帮助!
更新:2013年1月11日问题应该在于正确应用搜索谓词。 有几个实际上没有解决的相关问题。 Rails,Ransack:如何搜索HABTM关系中的“全部”匹配而不是“任何”
将ActiveRecord habtm查询转换为Arel
更新看起来问题是has_and_belongs_to_many https://github.com/ernie/ransack/issues/164无法解析的事件
你可以通过关联添加它,fe: class Person < ActiveRecord::Base PERSON_SEARCH_OPTIONS = :name_or_email_or_tags_name_cont end
它适用于我
我认为这是更好的方法。
链接地址: http://www.djcxy.com/p/35959.html上一篇: Ransack and acts
下一篇: Couldn't find UserSession without an ID in ruby on rails 3