如何在没有数据表列的情况下应用类方法范围?

时间:2021-07-22 16:20:28

Disclaimer: first question, rails newbie, please be detailed in responses.

免责声明:第一个问题,请在回复中详细说明。

I’m trying to create a filter with a class method that is based off a simple equation performed on two columns in my data table. I can’t figure out how to get the query/filter going such that it filters results based on the results of my equation. Here is an abbreviated set up of my table:

我正在尝试使用类方法创建一个过滤器,该类方法基于在我的数据表中的两列上执行的简单等式。我无法弄清楚如何让查询/过滤器进行,以便根据我的等式结果过滤结果。这是我的表的缩写设置:

t.integer   “horizontal_length” 
t.integer   “thirty_day_flow_rate”

I want a filter based off this equation: ( thirty_day_flow_rate / horizontal_length ), so users could say, "show me all the wells with a 30 day flow rate greater than 'x' barrels per foot of length"

我想要一个基于这个等式的滤波器:(thirty_day_flow_rate / horizo​​ntal_length),因此用户可以说,“向我展示30天流量大于每英尺长度'x'桶的所有井”

I have created a method in my model to hold the equation and it works fine when I call it on a Well object:

我在我的模型中创建了一个方法来保存方程式,当我在一个Well对象上调用它时它工作正常:

class Well < ActiveRecord::Base

def flow_rate_per_foot
  thirty_day_flow_rate / horizontal_length
end

However, when I want to create a filter based on the results of the equation, I am not sure how to proceed. I tried something like this, with the mimimum_flow_rate param passed in from the controller:

但是,当我想根据等式的结果创建一个过滤器时,我不知道如何继续。我试过这样的事情,从控制器传入mimimum_flow_rate参数:

class Well < ActiveRecord::Base

def self.flow_rate_filter(minimum_flow_rate)
  if mimimum_flow_rate.present?
    where(‘flow_rate_per_foot >= ?', minimum_flow_rate)
  else
    Well.all
  end
end

Obviously that does not work, because flow_rate_per_foot is not a column in my data table to be called. How can i work around this? Is there another way to solve this problem? I want to do this type of filtering for a number of different equations and columns, so any solution needs to be flexible. Thanks!

显然这不起作用,因为flow_rate_per_foot不是我的数据表中要调用的列。我该如何解决这个问题?有没有其他方法可以解决这个问题?我想对许多不同的方程和列进行这种类型的过滤,因此任何解决方案都需要灵活。谢谢!

For reference, my controller is shown below and other filters I have set up that run directly from my data table work properly:

作为参考,我的控制器如下所示,我设置的其他过滤器直接从我的数据表运行正常:

def index
   @wells = Well.flow_rate_filter(params[:mimimum_flow_rate])
end

Thanks!

3 个解决方案

#1


0  

you could try using scopes or just query it manually like

您可以尝试使用范围或只是手动查询

@wells = Well.where("(30_day_flow_rate / horizontal_length) >= ?", params[:mimimum_flow_rate])

@wells = Well.where(“(30_day_flow_rate / horizo​​ntal_length)> =?”,params [:mimimum_flow_rate])

#2


0  

What I wanted to do initially does not seem possible.

我最初想做的事似乎不可能。

I solved the problem by creating a new field in my table to hold the "flow_rate_per_foot" calculation that I had originally placed in the flow_rate_per_foot method.

我通过在表中创建一个新字段来解决问题,以保存我最初放在flow_rate_per_foot方法中的“flow_rate_per_foot”计算。

Once the result of the calculation had its own field, I could then filter, search and sort based on the results, which was my overall goal.

一旦计算结果有了自己的字段,我就可以根据结果进行过滤,搜索和排序,这是我的总体目标。

#3


-1  

Add this line in your model

在模型中添加此行

class Well < ActiveRecord::Base
attr_accessor :minimum_flow_rate
...

#1


0  

you could try using scopes or just query it manually like

您可以尝试使用范围或只是手动查询

@wells = Well.where("(30_day_flow_rate / horizontal_length) >= ?", params[:mimimum_flow_rate])

@wells = Well.where(“(30_day_flow_rate / horizo​​ntal_length)> =?”,params [:mimimum_flow_rate])

#2


0  

What I wanted to do initially does not seem possible.

我最初想做的事似乎不可能。

I solved the problem by creating a new field in my table to hold the "flow_rate_per_foot" calculation that I had originally placed in the flow_rate_per_foot method.

我通过在表中创建一个新字段来解决问题,以保存我最初放在flow_rate_per_foot方法中的“flow_rate_per_foot”计算。

Once the result of the calculation had its own field, I could then filter, search and sort based on the results, which was my overall goal.

一旦计算结果有了自己的字段,我就可以根据结果进行过滤,搜索和排序,这是我的总体目标。

#3


-1  

Add this line in your model

在模型中添加此行

class Well < ActiveRecord::Base
attr_accessor :minimum_flow_rate
...