宝石偏执狂-PG :: SyntaxError:错误:“ FROM”或附近的语法错误

时间:2018-08-13 11:01:58

标签: ruby-on-rails postgresql

在我的应用程序中,我有以下型号

fee.rb

class Fee < ActiveRecord::Base
  acts_as_paranoid
  belongs_to :instructor_student
  belongs_to :instructor
  has_many :fee_payment_notifications, dependent: :destroy
end

fee_payment_notifications.rb

class FeePaymentNotification < ActiveRecord::Base
  belongs_to :fee
end

instructor.rb

class Instructor < ActiveRecord::Base
  has_many :fees
  has_many :fee_payment_notifications, through: :fees
end

我想通过费用支付通知进行内部加入费用 当我直接在费用支付通知中使用费用加入通知时,出现以下错误

@instructor.fee_payment_notifications.joins(:fee)

PG::UndefinedTable: ERROR:  invalid reference to FROM-clause entry for table "fees"
LINE 1: ..."."id" = "fee_payment_notifications"."fee_id" AND "fees"."de...
                                                             ^
HINT:  Perhaps you meant to reference the table alias "fees_fee_payment_notifications".
: SELECT "fee_payment_notifications".* FROM "fee_payment_notifications" INNER JOIN "fees" "fees_fee_payment_notifications" ON "fees_fee_payment_notifications"."id" = "fee_payment_notifications"."fee_id" AND "fees"."deleted_at" IS NULL INNER JOIN "fees" ON "fee_payment_notifications"."fee_id" = "fees"."id" WHERE "fees"."deleted_at" IS NULL AND "fees"."instructor_id" = $1

可能是因为Deleted_at字段所致,所以我要使用以下查询进行手动联接

@instructor.fee_payment_notifications.select("fee_payment_notifications.* from fee_payment_notifications AS fp INNER JOIN fees as f ON fp.fee_id = f.id")

它给我下面的错误

PG :: SyntaxError:错误:“ FROM”或附近的语法错误 第1行:... ns作为fp内部联接费用,作为f ON fp.fee_id = f.id FROM“ fee _...                                                              ^     :从fee_payment_notifications AS中选择​​fee_payment_notifications。*作为fp INNER JOIN费用,如f ON fp.fee_id = f.id FROM“ fee_payment_notifications” INNER JOIN“ fees” ON“ fee_payment_notifications”。“ fee_id” =“ fees”。“ id” WHERE“费用“。” deleted_at“为NULL,”费用“。” instructor_id“ = $ 1     =>#

我应该如何编写查询以手动加入费用和费用支付通知?

1 个答案:

答案 0 :(得分:2)

这不是查询问题。这是因为paranoia宝石。 因此,您应该在模型中编写该代码。

acts_as_paranoid without_default_scope: true

请检查this链接。您可以找到添加默认范围