2

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

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

错误是:

PG::UndefinedTable: 错误: 缺少表“items”的 FROM 子句条目 LINE 1: 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...任何帮助将不胜感激。谢谢。

4

1 回答 1

5

找到答案了!

get_raw_records 中的查询应为:

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

于 2015-04-05T20:34:28.200 回答