【发布时间】:2015-02-16 16:05:46
【问题描述】:
所以,我有这两个模型:
class City < ActiveRecord::Base
has_and_belongs_to_many :companies
end
class Company < ActiveRecord::Base
has_and_belongs_to_many :cities
end
而连接表迁移是:
class CreateJoinCityCompany < ActiveRecord::Migration
def up
create_table :cities_companies do |t|
t.integer :city_id
t.integer :company_id
end
end
def down
drop_table :cities_companies
end
end
我正在尝试根据城市 ID 查找公司:
class CompaniesController < ApplicationController
def index
@companies = Company.includes(:cities).where('city.id' => params[:city_id]).references(:cities)
render :text => @companies.inspect
end
end
我得到的是:
PG::UndefinedTable: 错误: 缺少表“city”的 FROM 子句条目 第 1 行:...cities"."id" = "cities_companies"."city_id" WHERE "city"."id...^ : SELECT "companies"."id" AS t0_r0, "companies"."name" AS t0_r1, "公司"."category_id" AS t0_r2, "公司"."url" AS t0_r3, "公司"."created_at" AS t0_r4, "公司"."updated_at" AS t0_r5, "公司"."image_file_name" AS t0_r6, "公司"."image_content_type" AS t0_r7, "公司"."image_file_size" AS t0_r8, "公司"."image_updated_at" AS t0_r9, "cities"."id" AS t1_r0, "cities"."name" AS t1_r1, "城市"."state_id" AS t1_r2, "城市"."created_at" AS t1_r3, "cities"."updated_at" AS t1_r4 FROM "companies" 左外连接 "cities_companies" ON "cities_companies"."company_id" = "companies"."id" LEFT OUTER JOIN "cities" ON "cities"."id" = "cities_companies"."city_id" WHERE "city"."id" = '14'
我尝试了一些方法,例如将参数转换为 int,但我需要一些帮助来弄清楚这里发生了什么。
【问题讨论】:
标签: ruby-on-rails activerecord psql