简体   繁体   English

Rails 4中的Datagrid Gem返回PG:UndefinedColumn:错误:

[英]Datagrid Gem in Rails 4 returns PG:UndefinedColumn: ERROR:

No hair left on my head (and I have had lots :) ), I have been pulling out my hair and for the life of me I can't figure this out. 我的头上没有头发(而且我有很多:)),我一直在拔头发,对于我的一生,我无法弄清楚。

I have a one to many relations between 2 tables. 我在2张桌子之间有一对多关系。 I have installed the Datagrid Gem for reporting. 我已经安装了Datagrid Gem进行报告。 I need to get the report from one model based on the other one. 我需要从一种基于另一种模型的报告中获取报告。

Please have a look at my code. 请看一下我的代码。

reports_grid.rb report_grid.rb

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

reports_controller.rb report_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

Land.rb Land.rb

belongs_to :estate

estate.rb estate.rb

has_many :lands

Now when I go to /reports and try to run the filter I get the following error 现在,当我转到/ 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'

Why is the Gem tries to add "lands"."estate" = 'Olive Gardens' to the query when I have defined it at the instance. 当我在实例上定义查询时,为什么Gem会尝试在查询中添加"lands"."estate" = 'Olive Gardens'

Please let me know if you need me to add anything. 如果您需要我添加任何内容,请告诉我。 Thank you in advance. 先感谢您。

Edit: 编辑:

This is what I have done and worked in the Filter: I have done this: 这是我在过滤器中所做的工作:

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

Do you it is a good approach? 你是一个好方法吗? I guess in the scope I could say Land.joins(:estate) then use the scope.all.map... in the query. 我想在范围中我可以说Land.joins(:estate)然后在查询中使用scope.all.map...

Datagrid filter designed to filter data but not to just be by default. Datagrid过滤器旨在过滤数据,但并非默认设置。 If you have some reason why estate should not filter data by itself then add :dummy => true option: 如果您出于某种原因estate不应该自己过滤数据,请添加:dummy => true选项:

  filter(:estate, :enum, :select => ..., :dummy => true)

But I'would recommend it. 但我会推荐它。 Do this instead and your hair will start growing instantly: 改为这样做,您的头发将立即开始生长:

filter(:estate, :enum, :select => ...) do |scope, value|
  scope.joins(:estate).where("estates.title = ? ", value)
end

It seems obvious from documentation here: https://github.com/bogdan/datagrid/wiki/Filters#filter-block 从此处的文档看来很明显: https : //github.com/bogdan/datagrid/wiki/Filters#filter-block

尝试使用参考

Land.includes(:estate).references(:estates)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM