无法使用where子句rails检索信息

时间:2014-12-20 17:24:54

标签: ruby-on-rails postgresql activerecord

所以我有三张表:

  1. 用户
  2. 客户
  3. 预约
  4. Customer has_many AppointmentsCustomer 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的客户一起获取约会。

1 个答案:

答案 0 :(得分:3)

要在users表格上进行过滤,您需要将其包含在join中。尝试:

Appointment.joins([customer: :user]).where("users.email ILIKE test")

这将在appointmentscustomers以及customersusers之间创建一个联接,允许您过滤users表格列。