用户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
它会起作用......但我不喜欢那种方法!还有其他方法吗?