【问题标题】:How can I write an .includes() plus .where() on both top model and associated model in Rails4?如何在 Rails4 中的顶级模型和关联模型上编写 .includes() 和 .where()?
【发布时间】:2016-10-29 20:10:43
【问题描述】:

我不知道这个查询出了什么问题:

@customers =
      Customer.includes(:phone_numbers, :emails)
        .select("first_name, last_name, zip_code, customers.id, street1, street2")
        .where(:merchant_id => environment)
        .where("phone_numbers.number ~* :key",
               :key => key, :digits => digits)

它在 Rails3 中运行良好。

我偶然发现了一些文档 here,它们帮助我在 Rails4 中解决了这个问题,但我想知道是否有更好的方法来编写这个查询。

它产生以下内容(这很粗糙......):

"SELECT first_name, last_name, zip_code, customers.id, street1, street2, "customers"."id" AS t0_r0, "customers"."account_id" AS t0_r1, "customers"."first_name" AS t0_r2, "customers"."last_name" AS t0_r3, "customers"."street1" AS t0_r4, "customers"."street2" AS t0_r5, "customers"."city_id" AS t0_r6, "customers"."city" AS t0_r7, "customers"."state" AS t0_r8, "customers"."state_id" AS t0_r9, "customers"."zip_code" AS t0_r10, "customers"."country" AS t0_r11, "customers"."municipality" AS t0_r12, "customers"."latitude" AS t0_r13, "customers"."longitude" AS t0_r14, "customers"."location" AS t0_r15, "customers"."customer_type" AS t0_r16, "customers"."notes" AS t0_r17, "customers"."birthday" AS t0_r18, "customers"."merchant_id" AS t0_r19, "customers"."gmaps" AS t0_r20, "customers"."marketing" AS t0_r21, "customers"."communication_method" AS t0_r22, "customers"."account_status" AS t0_r23, "customers"."account_type" AS t0_r24, "customers"."created_at" AS t0_r25, "customers"."updated_at" AS t0_r26, "customers"."api_customer_id" AS t0_r27, "customers"."api_updated_at" AS t0_r28, "phone_numbers"."id" AS t1_r0, "phone_numbers"."customer_id" AS t1_r1, "phone_numbers"."account_id" AS t1_r2, "phone_numbers"."contact_id" AS t1_r3, "phone_numbers"."number" AS t1_r4, "phone_numbers"."country_code" AS t1_r5, "phone_numbers"."area_code" AS t1_r6, "phone_numbers"."extension" AS t1_r7, "phone_numbers"."created_at" AS t1_r8, "phone_numbers"."updated_at" AS t1_r9, "emails"."id" AS t2_r0, "emails"."customer_id" AS t2_r1, "emails"."account_id" AS t2_r2, "emails"."contact_id" AS t2_r3, "emails"."address" AS t2_r4, "emails"."created_at" AS t2_r5, "emails"."updated_at" AS t2_r6 
FROM "customers" 
LEFT OUTER JOIN "phone_numbers" ON "phone_numbers"."customer_id" = "customers"."id" 
LEFT OUTER JOIN "emails" ON "emails"."customer_id" = "customers"."id" 
WHERE "customers"."merchant_id" = 29 AND (phone_numbers.number ~* '424298')"

【问题讨论】:

    标签: ruby-on-rails postgresql arel


    【解决方案1】:

    由于您没有选择phone_numbersemails 中的任何字段,我认为您想使用join 而不是includes。棘手的一点是让LEFT OUTER 工作。

    http://apidock.com/rails/v4.2.1/ActiveRecord/QueryMethods/joins

    可能是这样的。

    Customer.
      select("first_name, last_name, zip_code, customers.id, street1, street2").
      joins("LEFT OUTER JOIN phone_numbers ON phone_numbers.customer_id = customers.id").
      joins("LEFT OUTER JOIN emails ON emails.customer_id = customers.id").
      where(:merchant_id => environment).
      where("phone_numbers.number ~* :key", :key => key, :digits => digits)
    

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 2017-03-23
      相关资源
      最近更新 更多