Rails范围返回重复项

时间:2013-08-20 16:23:50

标签: ruby-on-rails scope

我有一个返回重复项的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
  ]
 } 
}

1 个答案:

答案 0 :(得分:0)

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

MyTable.a_scope.where(something).uniq

在rails< 3: 你必须手动添加它,如下所示:

MyTable.find(:all, :select => "distinct my_table.*")