如何在 Rails 中向此 :select 添加 WHERE 语句?

发布于 2024-09-24 09:47:33 字数 965 浏览 3 评论 0 原文

我有当前的 find 语句:select:

@list = Contact.find :all,
  :select => "companies.name AS co_name,  
              COUNT(contact_emails.id) AS email_count, 
              COUNT(contact_calls.id) AS call_count, 
              COUNT(contact_letters.id) AS letter_count, 
              COUNT(contact_postalcards.id) AS postalcard_count,",
  :joins => [
    "LEFT JOIN companies ON companies.id = contacts.company_id",
    "LEFT JOIN contact_emails ON contact_emails.contact_id = contacts.id",
    "LEFT JOIN contact_letters ON contact_letters.contact_id = contacts.id",
    "LEFT JOIN contact_postalcards ON contact_postalcards.contact_id = contacts.id",
    "LEFT JOIN contact_calls ON contact_calls.contact_id = contacts.id"
  ],
  #:group => "companies.id"
   :group => "companies.name"

我只想提取那些 contact_email、contact_letters 等具有 date_sent < 的实例。 report_end_date 和 date_sent >报告开始日期。

我该怎么做?

I have the current find statement with :select:

@list = Contact.find :all,
  :select => "companies.name AS co_name,  
              COUNT(contact_emails.id) AS email_count, 
              COUNT(contact_calls.id) AS call_count, 
              COUNT(contact_letters.id) AS letter_count, 
              COUNT(contact_postalcards.id) AS postalcard_count,",
  :joins => [
    "LEFT JOIN companies ON companies.id = contacts.company_id",
    "LEFT JOIN contact_emails ON contact_emails.contact_id = contacts.id",
    "LEFT JOIN contact_letters ON contact_letters.contact_id = contacts.id",
    "LEFT JOIN contact_postalcards ON contact_postalcards.contact_id = contacts.id",
    "LEFT JOIN contact_calls ON contact_calls.contact_id = contacts.id"
  ],
  #:group => "companies.id"
   :group => "companies.name"

I only want to pull up those instances where contact_email, contact_letters, etcetera have date_sent < report_end_date and date_sent > report_start_date.

How do I do that?

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

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

发布评论

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

评论(1

零崎曲识 2024-10-01 09:47:33

通过添加条件。例如:

:conditions => ['contact_emails.date_sent < ? and contact_emails.date_sent > ?', report_end_date, report_start_date]

By adding conditions. For example:

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