Gem acts-as-taggable-on :match_all 出现的问题

miclle · 2011年12月31日 · 最后由 miclle 回复于 2012年03月13日 · 3864 次阅读

https://github.com/mbleigh/acts-as-taggable-on中有说:

# Find a user with matching all tags, not just one
User.tagged_with(["awesome", "cool"], :match_all => :true)

我在项目中需要用到这个,但是出现如下错误:生成的 SQL 语句有问题...

ruby-1.9.2-p180 :005 >   Solution.tagged_with(["气虚质", "老年"], :match_all => :true)
  ActsAsTaggableOn::Tag Load (0.8ms)  SELECT `tags`.* FROM `tags` WHERE (name LIKE '气虚质' ESCAPE '!' OR name LIKE '老年' ESCAPE '!')
  Solution Load (0.3ms)  SELECT `solutions`.* FROM `solutions` JOIN taggings solutions_taggings_283cd6c ON solutions_taggings_283cd6c.taggable_id = solutions.id AND solutions_taggings_283cd6c.taggable_type = 'Solution' AND solutions_taggings_283cd6c.tag_id = 23 JOIN taggings solutions_taggings_283cd6c ON solutions_taggings_283cd6c.taggable_id = solutions.id AND solutions_taggings_283cd6c.taggable_type = 'Solution' AND solutions_taggings_283cd6c.tag_id = 150 LEFT OUTER JOIN taggings solutions_taggings_group ON solutions_taggings_group.taggable_id = solutions.id AND solutions_taggings_group.taggable_type = 'Solution' GROUP BY solutions.id HAVING COUNT(solutions_taggings_group.taggable_id) = 2
Mysql2::Error: Not unique table/alias: 'solutions_taggings_283cd6c': SELECT `solutions`.* FROM `solutions` JOIN taggings solutions_taggings_283cd6c  ON solutions_taggings_283cd6c.taggable_id = solutions.id AND solutions_taggings_283cd6c.taggable_type = 'Solution' AND solutions_taggings_283cd6c.tag_id = 23 JOIN taggings solutions_taggings_283cd6c  ON solutions_taggings_283cd6c.taggable_id = solutions.id AND solutions_taggings_283cd6c.taggable_type = 'Solution' AND solutions_taggings_283cd6c.tag_id = 150 LEFT OUTER JOIN taggings solutions_taggings_group  ON solutions_taggings_group.taggable_id = solutions.id AND solutions_taggings_group.taggable_type = 'Solution' GROUP BY solutions.id HAVING COUNT(solutions_taggings_group.taggable_id) = 2
ActiveRecord::StatementInvalid: Mysql2::Error: Not unique table/alias: 'solutions_taggings_283cd6c': SELECT `solutions`.* FROM `solutions` JOIN taggings solutions_taggings_283cd6c  ON solutions_taggings_283cd6c.taggable_id = solutions.id AND solutions_taggings_283cd6c.taggable_type = 'Solution' AND solutions_taggings_283cd6c.tag_id = 23 JOIN taggings solutions_taggings_283cd6c  ON solutions_taggings_283cd6c.taggable_id = solutions.id AND solutions_taggings_283cd6c.taggable_type = 'Solution' AND solutions_taggings_283cd6c.tag_id = 150 LEFT OUTER JOIN taggings solutions_taggings_group  ON solutions_taggings_group.taggable_id = solutions.id AND solutions_taggings_group.taggable_type = 'Solution' GROUP BY solutions.id HAVING COUNT(solutions_taggings_group.taggable_id) = 2
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `query'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `block in exec_query'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:685:in `exec_query'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:679:in `select'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/base.rb:470:in `find_by_sql'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/relation.rb:114:in `block in to_a'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/identity_map.rb:72:in `without'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/relation.rb:113:in `to_a'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/activerecord-3.1.3/lib/active_record/relation.rb:436:in `inspect'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
    from /Users/zhengmiclle/.rvm/gems/ruby-1.9.2-p180@healthier/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'ruby-1.9.2-p180 :006 > 

我的 Gemfile 内容如下:

gem 'rails', '3.1.3'
gem 'mysql2', '0.3.11'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'devise', '1.5.3'
gem 'acts-as-taggable-on', '~> 2.2.1'
gem 'acts_as_tree', '0.1.1'
gem 'meta_search', '1.1.1'
gem 'jquery-rails', '1.0.19'
gem 'execjs', '1.2.13'
gem 'therubyracer', '0.9.9'
gem 'rails-i18n', '0.2.0'
gem 'fog', '1.1.2'
gem "kaminari", '0.13.0'
gem 'mini_magick', '3.3'
gem 'carrierwave', '0.5.8'
gem 'uuid', '2.3.4'
gem 'nokogiri', '1.5.0' 

同样遇到这个问题,搜到了这里,没看到解决。 我的解决办法是改写一下 acts-as-taggable-on 生成 alias 的方法 https://github.com/wanglian/acts-as-taggable-on

晚上我们 RubyTuesday 可以讨论,如果你来的话。

没有再用这个方法了,需求变了,另外不推荐用 match_all,生成的 SQL 太隐晦!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号