如何获得在帖子上评论(多态关联)的用户的DISTINCT列表?

时间:2012-06-13 20:50:40

标签: ruby-on-rails ruby-on-rails-3 activerecord

用户has_many评论。评论位于comments表(多态)。

User.where("comments.commentable_type = ? AND comments.commentable_id = ?", "Post", post.id).uniq

我明白了:

Post Load (0.4ms)  SELECT `posts`.* FROM `posts` LIMIT 1
TypeError: Cannot visit Arel::Nodes::Distinct
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:25:in `rescue in visit'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:133:in `visit_Arel_Nodes_SelectCore'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:41:in `visit_Arel_Nodes_SelectCore'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `block in visit_Arel_Nodes_SelectStatement'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `map'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `visit_Arel_Nodes_SelectStatement'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:36:in `visit_Arel_Nodes_SelectStatement'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:5:in `accept'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:19:in `accept'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/bind_visitor.rb:11:in `accept'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `to_sql'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/querying.rb:38:in `block in find_by_sql'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/explain.rb:40:in `logging_query_plan'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/querying.rb:37:in `find_by_sql'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:171:in `exec_queries'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:160:in `block in to_a'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/explain.rb:33:in `logging_query_plan'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:159:in `to_a'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:496:in `inspect'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
        from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'

没有.uniq,它可以工作,除了我得到重复,因此,我可以尝试:

User.where("comments.commentable_type = ? AND comments.commentable_id = ?", "Post", post.id).to_a.uniq

它会起作用......但我不喜欢那种方法!还有其他办法吗?

3 个答案:

答案 0 :(得分:1)

重启rails服务器会话。

答案 1 :(得分:1)

这是与您遇到的同一问题相关的github问题link

如果您使用的是rails 4,请尝试使用.distinct代替.uniq

答案 2 :(得分:0)

我记得有类似的问题,你可以做几件事。首先使用SQL distinct(我知道它与uniq类似)。第二个(我知道这不是最好的方法),你可以只选择它然后只使用常规数组uniq函数来过滤它就像你过滤普通数组一样(这只是为了让事情顺利)。