patrick
patrick

Reputation: 9722

how can I get a DISTINCT list of users who have commented (polymorphic association) on a post?

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

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

I get:

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'

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?

Upvotes: 1

Views: 2571

Answers (3)

Abhinaya
Abhinaya

Reputation: 809

This is a link to the github issue related to the same problem that you face.

If you are using rails 4 try using .distinct instead of .uniq .

Upvotes: 1

rrrhys
rrrhys

Reputation: 654

Restart the rails server session.

Upvotes: 2

YaBoyQuy
YaBoyQuy

Reputation: 793

I remember having issues similar, couple things you can do. First use the SQL distinct (I know it is suppose to be similar to the uniq). Second (I know this is not the best way), you can just select it all then just use regular array uniq functions to filter it just like how you would filter a normal array (this is to just get things going).

Upvotes: 0

Related Questions