rails:HABTM和ActiveRecord错误(PG :: UndefinedTable:错误:缺少FROM ...)

卢卡斯·罗伯斯克(Lucas lobosque)

因此,我有以下两种模型:

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”第1行的FROM子句条目:... cities“。” id“ =” cities_companies“。” city_id“在” city“。” id ... ^:SELECT “ company”。“ id” AS t0_r0,“ companies”。“名称” AS t0_r1,“ companies”。“ category_id” AS t0_r2,“ companies”。“ url” AS t0_r3,“ companies”。“ 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,“公司”。 “ cities”。“ id” AS t1_r0,“ cities”。“ name” AS t1_r1,“ cities”。“ state_id” AS t1_r2,“城市”。“ created_at” AS t1_r3,“城市”。“ updated_at” AS t1_r4从“ companies”左外联接“ cities_companies”在“ cities_companies”上。“ company_id” =“ companies “。” id“左外联接” cities“在” cities“上。” id“ =” cities_companies“。” city_id“在” city“。” id“ ='14'城市“。” id“ =”城市_公司“。” city_id“其中”城市“。” id“ ='14'城市“。” id“ =”城市_公司“。” city_id“其中”城市“。” id“ ='14'

我尝试了一些事情,例如将参数强制转换为int,但是我需要一些帮助来弄清楚这里发生了什么。

罗布·迪·马可(Rob Di Marco)

问题出在您的where条款中。

你引用city.id时,它应该是cities.id作为表名是城市。

更好的书写方式是使用哈希语法

Company.includes(:cities).where(cities: { id: params[:city_id] }).references(:cities)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails PG :: UndefinedTable:错误:缺少表的FROM子句条目

来自分类Dev

PG :: UndefinedTable:错误:关系不存在

来自分类Dev

Rails 4:嵌套属性和PG :: NotNullViolation错误

来自分类Dev

耙子流产了!PG :: UndefinedTable:错误:关系“页面”不存在

来自分类Dev

PG :: UndefinedTable:错误:在使用联接和位置时缺少表的FROM子句条目

来自分类Dev

Rails groupdate gem pg错误

来自分类Dev

PG :: UndefinedTable:错误:缺少FROM子句-订购带有“ includes” Rails的关联记录

来自分类Dev

Rails:PG :: UndefinedTable:错误:关系“ ...”不存在

来自分类Dev

Rails缺少模板错误

来自分类Dev

PG :: UndefinedTable:错误:关系“音乐家”不存在

来自分类Dev

频繁的PG :: ConnectionBad:RDS和rails / activerecord的连接关闭错误

来自分类Dev

PG :: UndefinedTable:错误:缺少表的FROM子句条目

来自分类Dev

Heroku上的Rails应用出现奇怪的PG :: UndefinedTable错误

来自分类Dev

如何使用WITH子查询更新或插入数据?并避免`PG :: UndefinedTable:错误:缺少表的FROM子句条目错误

来自分类Dev

耙子流产了!PG :: UndefinedTable:错误:关系“页面”不存在

来自分类Dev

使用rails的PG错误

来自分类Dev

Rails postgres错误:ActiveRecord :: StatementInvalid异常:PG :: AmbiguousColumn:错误:列引用“ sample_id”不明确

来自分类Dev

pg gem的Rails s和bundle安装错误?

来自分类Dev

ActiveRecord生成错误的关联HABTM名称

来自分类Dev

ActiveRecord :: StatementInvalid(PG :: UndefinedTable:错误:关系“ guestbooks”不存在

来自分类Dev

PG :: UndefinedTable:错误:缺少表的FROM子句条目

来自分类Dev

Rails 4:使用父实体表联接查询,错误:PG :: UndefinedTable:错误:缺少表“状态”的FROM子句条目

来自分类Dev

ActiveRecord :: StatementInvalid(PG :: UndefinedTable:错误:关系“ spree_orders”不存在

来自分类Dev

PG :: UndefinedTable错误在Rails Heroku上的红宝石

来自分类Dev

Rails:ActiveRecord::AssociationTypeMismatch 错误

来自分类Dev

PG::UndefinedTable:错误:关系“洞穴”不存在

来自分类Dev

PG::UndefinedTable:错误:关系“”不存在 - Amazon EC2、Postgresql 和 Rails 5

来自分类Dev

生产中的 Heroku 数据库错误:PG::UndefinedTable 错误

来自分类Dev

Heroku 迁移失败 UndefinedTable 错误

Related 相关文章

  1. 1

    Rails PG :: UndefinedTable:错误:缺少表的FROM子句条目

  2. 2

    PG :: UndefinedTable:错误:关系不存在

  3. 3

    Rails 4:嵌套属性和PG :: NotNullViolation错误

  4. 4

    耙子流产了!PG :: UndefinedTable:错误:关系“页面”不存在

  5. 5

    PG :: UndefinedTable:错误:在使用联接和位置时缺少表的FROM子句条目

  6. 6

    Rails groupdate gem pg错误

  7. 7

    PG :: UndefinedTable:错误:缺少FROM子句-订购带有“ includes” Rails的关联记录

  8. 8

    Rails:PG :: UndefinedTable:错误:关系“ ...”不存在

  9. 9

    Rails缺少模板错误

  10. 10

    PG :: UndefinedTable:错误:关系“音乐家”不存在

  11. 11

    频繁的PG :: ConnectionBad:RDS和rails / activerecord的连接关闭错误

  12. 12

    PG :: UndefinedTable:错误:缺少表的FROM子句条目

  13. 13

    Heroku上的Rails应用出现奇怪的PG :: UndefinedTable错误

  14. 14

    如何使用WITH子查询更新或插入数据?并避免`PG :: UndefinedTable:错误:缺少表的FROM子句条目错误

  15. 15

    耙子流产了!PG :: UndefinedTable:错误:关系“页面”不存在

  16. 16

    使用rails的PG错误

  17. 17

    Rails postgres错误:ActiveRecord :: StatementInvalid异常:PG :: AmbiguousColumn:错误:列引用“ sample_id”不明确

  18. 18

    pg gem的Rails s和bundle安装错误?

  19. 19

    ActiveRecord生成错误的关联HABTM名称

  20. 20

    ActiveRecord :: StatementInvalid(PG :: UndefinedTable:错误:关系“ guestbooks”不存在

  21. 21

    PG :: UndefinedTable:错误:缺少表的FROM子句条目

  22. 22

    Rails 4:使用父实体表联接查询,错误:PG :: UndefinedTable:错误:缺少表“状态”的FROM子句条目

  23. 23

    ActiveRecord :: StatementInvalid(PG :: UndefinedTable:错误:关系“ spree_orders”不存在

  24. 24

    PG :: UndefinedTable错误在Rails Heroku上的红宝石

  25. 25

    Rails:ActiveRecord::AssociationTypeMismatch 错误

  26. 26

    PG::UndefinedTable:错误:关系“洞穴”不存在

  27. 27

    PG::UndefinedTable:错误:关系“”不存在 - Amazon EC2、Postgresql 和 Rails 5

  28. 28

    生产中的 Heroku 数据库错误:PG::UndefinedTable 错误

  29. 29

    Heroku 迁移失败 UndefinedTable 错误

热门标签

归档