简体   繁体   English

如何在Rails4的顶级模型和关联模型上编写.includes()和.where()?

[英]How can I write an .includes() plus .where() on both top model and associated model in Rails4?

I didn't have any idea what was going wrong with this query: 我不知道此查询出了什么问题:

@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)

It was working fine in Rails3. 在Rails3中运行正常。

I stumbled onto some documentation here that helped me resolve it in Rails4, but I'd like to know if there is a better way to write this query. 我在这里偶然发现了一些文档,这些文档可以帮助我在Rails4中解决它,但是我想知道是否有更好的方法来编写此查询。

It produces the following (this is gnarly...): 它会产生以下内容(这太过分了……):

"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')"

Since you aren't selecting any of the fields in phone_numbers or emails I think you want to use join not includes . 由于您没有选择phone_numbersemails任何字段,因此我认为您要使用join not includes Tricky bit is to make the LEFT OUTER work. 棘手的一点是使LEFT OUTER起作用。

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

Something like this maybe. 大概是这样的。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取模型实例及其包含物,以及包含物的包含物? - How can I fetch a model instance, plus its includes, plus the includes' includes? Rails4:在fields_for部分中引用关联的模型 - Rails4 : Referencing associated model in fields_for partial Rails4:如何对检索到的Model.all数据使用截断方法? - Rails4: How can I use truncate method for the data retrieved Model.all? ruby2 / rails4:如何在模型中设置一个变量,然后该变量可用于多种方法 - ruby2/rails4: How do I set a variable in a model that can then be available to multiple methods 如何在Ruby on Rails4上为has_many模型下订单? - How can I make order for has_many model on Ruby on Rails4? Rails4应用模型 - Rails4 Application Model 如何在使用其他模型对该字段进行打字的文本字段上编写自定义rails验证? - How can I write custom rails validations on a text field where I typecast this field using another model? 如何编写包含第3个模型并由第3个模型过滤的Rails查询? - How to write a rails query which includes and filter by a 3rd model? 我在哪里为邮件中的关联模型定义该rails方法? - where do I define this rails method for an associated model in a mailer? 模型更新时出现Rails4:ForbiddenAttributesError - Rails4: ForbiddenAttributesError on Model Update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM