Ajax 数据表 Rails - 查询不适用于显示方法
Ajax Datatables Rails - Query not working for show-method
让我们从数据库模型开始:
# => ProductSelection.rb
has_many :products, -> { uniq }, through: :product_variants
# => Product.rb
has_many :product_informations
belongs_to :product_configuration
belongs_to :product_class
在 Rails 上使用纯 Ruby,我们收集了 products
以在 product_selection#show
方法中显示,如下所示:
@products = ProductSelection.find(params[:id]).products.includes(:product_informations, :product_class, :product_configuration)
并生成了一个 table 像这样:
= table(@products).as(:products).default do |product|
= product.name
= product.product_configuration.name
= product.product_class.name
= product.state
= link_to product_product_selection_path(@product_selection, product_id: product.id), method: :delete
现在我们想在 Rails.
上使用 Datatable 而不是普通的 Ruby
我们正在使用Ajax-Datatables-Rails-Gem。我们希望所有列最后都是 sortable 和可搜索的。
不幸的是,我们没有通过查询来检索属于有问题的 ProductSelection
的 Products
。
这是我们到目前为止尝试过的查询:
def get_raw_records
ProductSelection.find(params[:id]).products.includes(:product_informations, :product_class, :product_configuration).references(:product_information, :product_class, :product_configuration).distinct
end
def data
records.map do |record|
[
record.name,
record.product_configuration.name,
record.product_class.name,
record.state,
record.id
]
end
end
弹出的错误:
> undefined method `each_with_index' for nil:NilClass
添加 sortable/searchable 列时,错误如下:
PG::UndefinedColumn at /product_selections/fetch_table_data_show.json
=====================================================================
> ERROR: column products.name does not exist
LINE 1: SELECT DISTINCT "products"."id", products.name
配置如下:
def sortable_columns
# Declare strings in this format: ModelName.column_name
@sortable_columns ||= %w(Product.name ProductConfiguration.name ProductClass.name Product.state)
end
def searchable_columns
# Declare strings in this format: ModelName.column_name
@searchable_columns ||= %w(Product.name ProductConfiguration.name ProductClass.name Product.state)
end
我认为问题出在不同的模型上。我假设 Datatables-Rails 需要一个 ProductSelection
的模型,但提示是 Product
。任何帮助将不胜感激!
如果有任何遗漏,请告诉我!
再看一遍之后,我们发现问题在于 Product.name
不是有效的 ActiveRecord 构造。相反,它是在某个地方定义的辅助方法。
由于 gem 尝试通过列名查找记录,因此发生了此错误。
我们通过删除有问题的列解决了这个问题。其他方法是:
- 将数据存储在单独的列中,而不是使用助手。
- 实施 javascript 中的助手行为,以便我们可以通过
它作为对数据表的回调。
让我们从数据库模型开始:
# => ProductSelection.rb
has_many :products, -> { uniq }, through: :product_variants
# => Product.rb
has_many :product_informations
belongs_to :product_configuration
belongs_to :product_class
在 Rails 上使用纯 Ruby,我们收集了 products
以在 product_selection#show
方法中显示,如下所示:
@products = ProductSelection.find(params[:id]).products.includes(:product_informations, :product_class, :product_configuration)
并生成了一个 table 像这样:
= table(@products).as(:products).default do |product|
= product.name
= product.product_configuration.name
= product.product_class.name
= product.state
= link_to product_product_selection_path(@product_selection, product_id: product.id), method: :delete
现在我们想在 Rails.
上使用 Datatable 而不是普通的 Ruby我们正在使用Ajax-Datatables-Rails-Gem。我们希望所有列最后都是 sortable 和可搜索的。
不幸的是,我们没有通过查询来检索属于有问题的 ProductSelection
的 Products
。
这是我们到目前为止尝试过的查询:
def get_raw_records
ProductSelection.find(params[:id]).products.includes(:product_informations, :product_class, :product_configuration).references(:product_information, :product_class, :product_configuration).distinct
end
def data
records.map do |record|
[
record.name,
record.product_configuration.name,
record.product_class.name,
record.state,
record.id
]
end
end
弹出的错误:
> undefined method `each_with_index' for nil:NilClass
添加 sortable/searchable 列时,错误如下:
PG::UndefinedColumn at /product_selections/fetch_table_data_show.json
=====================================================================
> ERROR: column products.name does not exist
LINE 1: SELECT DISTINCT "products"."id", products.name
配置如下:
def sortable_columns
# Declare strings in this format: ModelName.column_name
@sortable_columns ||= %w(Product.name ProductConfiguration.name ProductClass.name Product.state)
end
def searchable_columns
# Declare strings in this format: ModelName.column_name
@searchable_columns ||= %w(Product.name ProductConfiguration.name ProductClass.name Product.state)
end
我认为问题出在不同的模型上。我假设 Datatables-Rails 需要一个 ProductSelection
的模型,但提示是 Product
。任何帮助将不胜感激!
如果有任何遗漏,请告诉我!
再看一遍之后,我们发现问题在于 Product.name
不是有效的 ActiveRecord 构造。相反,它是在某个地方定义的辅助方法。
由于 gem 尝试通过列名查找记录,因此发生了此错误。
我们通过删除有问题的列解决了这个问题。其他方法是:
- 将数据存储在单独的列中,而不是使用助手。
- 实施 javascript 中的助手行为,以便我们可以通过 它作为对数据表的回调。