本文介绍了我怎么能得到用户的谁也评论(多态关联)一则讯息的DISTINCT列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户的has_many 的意见。评论是在评论表(多态)​​。

User has_many comments. Comment are in the comments table (polymorphic).

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 ,它的工作原理,但我得到的重复,因此,我可以试试:

Without .uniq, it works, except I get duplicates, therefore, I can try:

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

和它的工作......但我不喜欢这样的方法!有没有其他办法?

And it will work...but I don't like that method! Is there any other way?

推荐答案

重新启动轨道服务器会话。

Restart the rails server session.

这篇关于我怎么能得到用户的谁也评论(多态关联)一则讯息的DISTINCT列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:29