【问题标题】:Missing FROM-clause with DataTables Global Search缺少 DataTables 全局搜索的 FROM 子句
【发布时间】:2015-04-04 14:45:32
【问题描述】:

我正在使用datatables gem 来显示一个包含两个相关表的表。结果,它从三个不同的表中拉入并显示列。

这工作正常,我什至可以按所需的列进行排序。不幸的是,全局搜索功能已损坏。似乎 SQL 语句的格式不正确。它没有找到执行 where 子句的表。

错误是:

PG::UndefinedTable: 错误:缺少表“items”第 1 行的 FROM 子句条目:SELECT COUNT() FROM "item_stores" WHERE ((CAST("items"."de... ^ : SELECT COUNT() FROM "item_stores" WHERE ((CAST("items"."description" AS VARCHAR) ILIKE '%b%' OR CAST("stores"."store_name" AS VARCHAR) ILIKE '%b %'))

我使用的模型位于其他两个之间,与每个模型具有多对一的关系:Items(1)-(M)ItemStores(M)-(1)Stores。

我的原始数据查询是:

def get_raw_records
  # insert query here
  ItemStore.includes([ :item, :store ]).all 
end

index.json.builder

json.item_stores @item_stores do |item_store|
  json.id item_stores.id
  json.item.image_path item_store.item.image_path
  json.store_id item_stores.store_id
  json.price item_stores.price

  json.items item_store.items do |item|
    json.(item, :description, :price, :image_path)
  end

  json.stores item_store.stores do |store|
    json.(store, :store_name)
  end

  json.url item_stores_url(i, format: :json)
end

我无法弄清楚我可以做些什么来修复附加到全局搜索的基础 SQL...任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 谢谢。刚遇到同样的问题。文档中没有!

标签: ruby-on-rails jquery-datatables-rails


【解决方案1】:

找到答案!

get_raw_records 中的查询应为:

ItemStore.includes(:item, :store).references(:item, :store)

【讨论】:

  • 添加引用似乎会破坏它。如果没有references,页面会在 2 秒内加载,但在尝试搜索时会抛出所描述的错误。使用references,它永远不会加载。我已经等了半个小时了。我想知道有没有其他人看过这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 2015-10-30
  • 2015-11-07
  • 2020-04-30
相关资源
最近更新 更多