1

用户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

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

4

3 回答 3

1

这是与您面临的相同问题相关的 github 问题的链接。

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

于 2013-10-02T18:46:18.030 回答
1

重新启动 rails 服务器会话。

于 2013-04-15T20:45:29.070 回答
0

我记得有类似的问题,你可以做几件事。首先使用 SQL distinct(我知道它应该类似于 uniq)。其次(我知道这不是最好的方法),您可以将其全部选中,然后使用常规数组 uniq 函数来过滤它,就像您过滤普通数组一样(这只是为了让事情顺利进行)。

于 2012-06-14T16:38:18.930 回答