所以我有三张表:
Customer
has_many Appointments
,Customer
belongs_to User
我必须根据Appointments
的{{1}}检索email
。我在customer
表格中有email
字段。
我尝试了以下查询:
User
给了我这个错误:
Appointment.joins(:customer).where("customers.users.email ILIKE test")
我尝试添加这个:
Appointment Load (1.3ms) SELECT "appointments".* FROM "appointments" INNER JOIN "customers" ON "customers"."id" = "appointments"."customer_id" WHERE (customers.users.email ILIKE test)
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "users"
LINE 1: ...omers"."id" = "appointments"."customer_id" WHERE (customers....
^
: SELECT "appointments".* FROM "appointments" INNER JOIN "customers" ON "customers"."id" = "appointments"."customer_id" WHERE (customers.users.email ILIKE test)
以及
belongs_to :customer, inverse_of: :appointments
belongs_to :user, through: :customer
显然这些都不起作用。我也在客户模型中试过这个:
has_one :customer_user, through: :customer
和查询:
delegate :email, to: :user, prefix: true
引发以下错误:
User.joins(:customer).where("customers.user_email ILIKE test")
如何与具有特定电子邮件ID的客户一起获取约会。
答案 0 :(得分:3)
要在users
表格上进行过滤,您需要将其包含在join
中。尝试:
Appointment.joins([customer: :user]).where("users.email ILIKE test")
这将在appointments
和customers
以及customers
和users
之间创建一个联接,允许您过滤users
表格列。