我有很多客戶的公司。客戶has_many消息。消息具有文本屬性。如何從一個模型中獲取一個基於關聯到另一個模型的ID列表
我試圖從沒有在消息的文本屬性中包含特定單詞的客戶獲取customer_ids的列表。這是我到目前爲止有:
company.customers.includes(:messages).references(:messages).where('messages.text NOT LIKE ?', "% summer %")
,當我做到這一點,得到該組中的客戶的數量,這是正確的號碼(7500),但是當我去掐了IDS像這樣:
company.customers.includes(:messages).references(:messages).where('messages.text NOT LIKE ?', "% summer %").pluck(:id).count
返回80000分一樣的結果,這是不正確
我到底做錯了什麼?
編輯:
PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous
LINE 1: SELECT id FROM "customers" LEFT OUTER JOIN "messages" ON "me...
^
: SELECT id FROM "customers" LEFT OUTER JOIN "messages" ON "messages"."customer_id" = "customers"."id" WHERE "customers"."company_id" = $1 AND (messages.text NOT LIKE '% summer %')
PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous
LINE 1: SELECT id FROM "customers" LEFT OUTER JOIN "messages" ON "me...
^
: SELECT id FROM "customers" LEFT OUTER JOIN "messages" ON "messages"."customer_id" = "customers"."id" WHERE "customers"."company_id" = $1 AND (messages.text NOT LIKE '% summer %')
ActiveRecord::StatementInvalid: PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous
LINE 1: SELECT id FROM "customers" LEFT OUTER JOIN "messages" ON "me...
如果刪除了'.references什麼:從查詢'(消息)? ('.joins'觸發一個'INNER JOIN'來創建重複項,'.includes'觸發'LEFT OUTER JOIN'不會創建重複項) – MrYoshiji
但我沒有使用.joins ...我會在哪裏放置? –
只需嘗試'company.customers.includes(:messages).where('messages.text NOT LIKE?',「%summer%」)。count'來查看它是否有效。如果是這樣,我會發布解決問題的答案,這裏 – MrYoshiji