FROM句のエラーがありません(以下を参照)
関連するレポートの都市がユーザーの都市と同じであるエントリを検索したいので、ユーザーに関連するエントリのみを表示します。したがって、ユーザーがレポートを行う都市に属している場合、そのユーザーにはそのレポートのエントリが表示されます。
私はいくつかのことを試しました。1つはエントリモデルのこのスコープです。
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を初めて使用するので、このクエリに関するアドバイスをいただければ幸いです。