Ruby on rails Rails 4中的Datagrid Gem返回PG:UndefinedColumn:ERROR:
我的头上没有头发了(我有很多:),我一直在拔我的头发,我一辈子都搞不清楚这一点 我在两个表之间有一对多关系。我已经安装了用于报告的Datagrid Gem。我需要根据另一个模型从一个模型得到报告 请看一下我的代码 报告\u grid.rbRuby on rails Rails 4中的Datagrid Gem返回PG:UndefinedColumn:ERROR:,ruby-on-rails,datagrid,reporting,Ruby On Rails,Datagrid,Reporting,我的头上没有头发了(我有很多:),我一直在拔我的头发,我一辈子都搞不清楚这一点 我在两个表之间有一对多关系。我已经安装了用于报告的Datagrid Gem。我需要根据另一个模型从一个模型得到报告 请看一下我的代码 报告\u grid.rb class ReportsGrid include Datagrid scope do Land.includes(:estate) end filter(:estate, :enum, :select => proc { E
class ReportsGrid
include Datagrid
scope do
Land.includes(:estate)
end
filter(:estate, :enum, :select => proc { Estate.group("title").select("title").map {|c| [c.title] }})
column(:id, :header => "Land ID")
column(:current_stage, :header => "Stage")
column(:price)
column(:status)
end
报告\u controller.rb
class ReportsController < ApplicationController
def index
@grid = ReportsGrid.new(params[:reports_grid]) do |scope|
if params[:reports_grid].present?
if params[:reports_grid][:estate].present?
scope.joins(:estate).where("estates.title = ? ",params[:reports_grid][:estate]).page(params[:page])
**# when I get the @grid.assets here all good and return correct number of rows**
else
scope.page(params[:page])
end
else
scope.page(params[:page])
end
end
end
end
estate.rb
has_many :lands
现在,当我转到/reports并尝试运行过滤器时,我得到以下错误
PG::UndefinedColumn: ERROR: column lands.estate does not exist LINE 1: ..._id" WHERE (estates.title = 'Olive Gardens' ) AND "lands"."e... ^ : SELECT COUNT(*) FROM "lands" INNER JOIN "estates" ON "estates"."id" = "lands"."estate_id" WHERE (estates.title = 'Olive Gardens' ) AND "lands"."estate" = 'Olive Gardens'
为什么当我在实例中定义了查询时,Gem会尝试将“lands”、“estate”=“Olive Gardens”
添加到查询中
如果你需要我补充什么,请告诉我。先谢谢你
编辑:
这就是我在过滤器中所做的工作:
我已经这样做了:
filter(:estate_id, :enum,
:select => lambda {Estate.all.map {|p| [p.title, p.id]}},
:multiple => false,
:include_blank => true
) do |value|
self.where(:lands => {:estate_id => value})
end
你认为这是一个好方法吗?
我想在范围中,我可以说
Land.joins(:estate)
,然后在查询中使用scope.all.map…
。Datagrid筛选器设计用于筛选数据,但不只是默认情况下使用。
如果您有某种原因认为地产
不应单独过滤数据,请添加:dummy=>true
选项:
filter(:estate, :enum, :select => ..., :dummy => true)
但我会推荐它。改为这样做,您的头发将立即开始生长:
filter(:estate, :enum, :select => ...) do |scope, value|
scope.joins(:estate).where("estates.title = ? ", value)
end
从这里的文档中可以明显看出:
尝试使用引用
Land.includes(:estate).references(:estates)
Land.includes(:estate).references(:estates)