简体   繁体   English

控制器查询中的可选Rails参数

[英]Optional Rails Params in Controller Query

I have a question in regards to Rails params. 我对Rails参数有疑问。 I currently have only one filter on one of my views that allows users to filter data by a date range. 我目前在一个视图上只有一个过滤器,该过滤器允许用户按日期范围过滤数据。 I am adding two more filters to that view so that users can filter by code and country. 我要在该视图中添加两个过滤器,以便用户可以按代码和国家/地区过滤。 However, I want those filters to be optional. 但是,我希望这些过滤器是可选的。

My current query looks something like this: 我当前的查询看起来像这样:

@data = Games
      .where("date BETWEEN ? AND ?", *date_range_array)
      .includes(:synced_country)
      .order(sort_column + " " + sort_direction)
      .page(params[:page])

The params for code and country will be something like, params[:code] and params[:country]. 用于代码和国家的参数将类似于params [:code]和params [:country]。 I would like to put them in the query like: 我想将它们放在查询中,例如:

  @data = Games
          .where("date BETWEEN ? AND ?", *date_range_array)
          .where("unique_code in ?" params[:code])
          .where("country in ?" params[:country])
          .includes(:synced_country)
          .order(sort_column + " " + sort_direction)
          .page(params[:page])

The issue I am having is that if the user does not input anything for params[:code] and params[:country] I get an error because they are nil. 我遇到的问题是,如果用户未为params [:code]和params [:country]输入任何内容,则会因为它们为nil而出现错误。 Any idea on how to handle this kind of situation? 关于如何处理这种情况有什么想法?

I would build it incrementally: 我将逐步构建它:

@data = Games.where("date BETWEEN ? AND ?", *date_range_array)

@data = @data.where("unique_code in ?", params[:code]) if params[:code]
@data = @data.where("country in ?", params[:country])  if params[:country]

@data = @data.includes(:synced_country)
             .order(sort_column + " " + sort_direction)
             .page(params[:page])

Note that you are missing two commas right before params[:code] and params[:country] which I've fixed in the above code. 请注意,在上面的代码中已修复的params[:code]params[:country]之前,您缺少两个逗号。

@data = Games.where("date BETWEEN ? AND ?", *date_range_array).scoped
@data = @data.where("unique_code in ?" params[:code]).scoped if params[:code].present?
@data = @data.where("country in ?" params[:country]).scoped if params[:coutry].present?
@data = @data.includes(:synced_country)
             .order(sort_column + " " + sort_direction)
             .page(params[:page])

Anyway, I would move that complex query to a named scope or something inside the Game Model. 无论如何,我会将那个复杂的查询移到一个命名范围或游戏模型中的某个东西。

EDIT: added the "scoped" method, i'm not sure if needed, try it without .scoped if you want 编辑:添加了“作用域”的方法,我不确定是否需要,如果您想要没有.scoped

I usually use the same method with Agis. 我通常对Agis使用相同的方法。 But sometimes I trend to use scope(as arieljuod said): 但有时我倾向于使用范围(如arieljuod所说):

class Games

  scope :in_contry, lambda{|contry| where("contry in ?", contry) if contry.present?}

  scope :in_code, lambda{|code| where("unique_code in ?", code) if code.present?}

  //other codes
end

//call lambdas as follows:
@data = Games.where("date BETWEEN ? AND ?", *date_range_array)
          .in_code(params[:code])
          .in_contry(params[:country])
          .includes(:synced_country)
          .order(sort_column + " " + sort_direction)
          .page(params[:page])

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

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