命名范围与 find_by_sql (具体示例)

发布于 2024-08-13 09:37:13 字数 634 浏览 3 评论 0原文

出于好奇,有人知道使用命名范围(而不是 find_by_sql )构建以下集合的更好方法吗?

@available = Workflow.find_by_sql(["
      SELECT workflows.id FROM workflows
      WHERE workflows.project_id = ? AND workflows.status < 5 AND
      ( workflows.created_by = ? OR workflows.id IN 
        (
           SELECT workflow_id FROM workflow_histories   
           INNER JOIN workflow_recipients on workflow_histories.id = workflow_recipients.workflow_history_id   
           WHERE workflow_recipients.recipient_id = ? AND workflow_recipients.recipient_type = ?
        )
      )", project.id, @current_user.id,  @current_user.id , 'USER'])

Just out of curiosity, does anyone know a better way of building the following collection using named scopes (as opposed to find_by_sql)?

@available = Workflow.find_by_sql(["
      SELECT workflows.id FROM workflows
      WHERE workflows.project_id = ? AND workflows.status < 5 AND
      ( workflows.created_by = ? OR workflows.id IN 
        (
           SELECT workflow_id FROM workflow_histories   
           INNER JOIN workflow_recipients on workflow_histories.id = workflow_recipients.workflow_history_id   
           WHERE workflow_recipients.recipient_id = ? AND workflow_recipients.recipient_type = ?
        )
      )", project.id, @current_user.id,  @current_user.id , 'USER'])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海里的那抹蓝 2024-08-20 09:37:13

我还没有测试过这个,但我认为它会起作用:(

named_scope :available, lambda { |user_id, project_id|
  { :select => :id,
    :conditions =>  [ "project_id = :project_id AND status < 5 AND
                       (created_by = :user_id OR id IN (
                         SELECT workflow_id FROM workflow_histories   
                         INNER JOIN workflow_recipients ON workflow_histories.id = workflow_recipients.workflow_history_id   
                         WHERE workflow_recipients.recipient_id = :user_id AND workflow_recipients.recipient_type = :recipient_type
                       )",
                      { :user_id        => user_id,
                        :project_id     => project_id,
                        :recipient_type => "USER"
                      }
                    ]
  }    
}

我的答案的先前版本将子选择分解为它自己的查询,我认为这是不必要的。)

I haven't tested this, but I think it would work:

named_scope :available, lambda { |user_id, project_id|
  { :select => :id,
    :conditions =>  [ "project_id = :project_id AND status < 5 AND
                       (created_by = :user_id OR id IN (
                         SELECT workflow_id FROM workflow_histories   
                         INNER JOIN workflow_recipients ON workflow_histories.id = workflow_recipients.workflow_history_id   
                         WHERE workflow_recipients.recipient_id = :user_id AND workflow_recipients.recipient_type = :recipient_type
                       )",
                      { :user_id        => user_id,
                        :project_id     => project_id,
                        :recipient_type => "USER"
                      }
                    ]
  }    
}

(A previous version of my answer breaks the sub-select out into its own query, which I think is unnecessary.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文