我缺少 FROM 子句错误(如下所示)
我想找到关联报告的城市与用户城市相同的条目,因此只向用户显示相关条目。因此,如果用户属于报告所在的城市,则应向用户显示该报告中的条目。
我尝试了一些东西,其中一个是 Entry 模型中的这个范围:
scope :report_in_city, lambda { |user| joins(:report).where("report.city_id = ?", user.city_id) }
如果我打电话Entry.report_in_city(user)
我收到此错误:
SELECT "entries".* FROM "entries" INNER JOIN "reports" ON "reports"."id" = "entries"."report_id" WHERE (report.city_id = 1)
ActiveRecord::StatementInvalid: PG::Error: ERROR: missing FROM-clause entry for table "report"
LINE 1: ... ON "reports"."id" = "entries"."report_id" WHERE (report.cit...
^
: SELECT "entries".* FROM "entries" INNER JOIN "reports" ON "reports"."id" = "entries"."report_id" WHERE (report.city_id = 1)
我有几个这样设置的模型:
class Report
belongs_to user
belongs_to city
has_many entries
class Entry
belongs_to report
class User
has_many reports
belongs_to city
class City
has_many users
has_many reports
我是 sql 新手,不胜感激有关此查询的任何建议!