1

我有一个返回重复项的 Rails 范围查询。谁能看到它有什么问题?这是代码:

scope :visible_to_user, lambda { |user|
 {
  :joins      => 'LEFT JOIN SCHEMA.groups ON groups.id = uploaded_files.group_id
                  LEFT JOIN uploaded_file_references ON uploaded_files.id = uploaded_file_references.uploaded_file_id
                  LEFT JOIN message_threads ON message_threads.id = uploaded_file_references.thread_id
                  LEFT JOIN thread_participants ON thread_participants.message_thread_id = message_threads.id',
  :conditions => [
    %{
        uploaded_files.in_private_conversation = false
        AND ( ( NOT COALESCE(groups.private, false) ) 
              OR uploaded_files.group_id IN (?) 
              OR ( thread_participants.referenced_id = '?' 
                   AND thread_participants.referenced_type = 'User')          
            )
    }, user.group_ids, user.id
  ]
 } 
}
4

1 回答 1

0

在 rails >= 3 中:您可以将 .uniq 调用添加到任何范围以不返回重复项,如下所示:

MyTable.a_scope.where(something).uniq

在 rails < 3 中:您必须使用如下所示的选择配置手动添加它:

MyTable.find(:all, :select => "distinct my_table.*")
于 2013-08-21T11:54:29.960 回答