扶手:一个模型链接过滤方法 [英] Rails: Chaining filter methods on a model

查看:106
本文介绍了扶手:一个模型链接过滤方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的web应用程序是显示的文章列表。文章可以发表在不同的国家(即文章属于国家,国家有很多文章),并在不同的语言(即文章属于语言和语言有许多文章)。

我想先做一个请求将返回我的文章来自特定国家和特定语言。也就是说,我最终想提出一个要求是这样:

Article.selected_countries.selected_languages​​

和得到的文章列表。

既为国家,和所采用的语言,前端可以发送以下参数:

  • 全部 - 这意味着,有效的,不适用此过滤器,并返回来自所有国家的文章(或文章所有语言);
  • 的ID的数组 - 这将意味着,仅返回来自国家文章所提供的ID(或语言所提供的ID)
  • 在空数组 - 我想,这是previous选项的特殊情况;这将意味着,从没有一个国家的文章(或在任何语言)都被退回,所以没有物品将被退回

现在,什么混淆我是怎么写这些类的方法和如何使它们可链接。 Rails的指南提供下面的例子:

 类文章<的ActiveRecord :: Base的
  高清self.created_before(时间)
    其中(created_at&所述;?,时间)
  结束
结束
 

如果我建立一个基于本实施例的一类方法,如下列:

 高清self.selected_countries(参数)
  ???
结束
 

如何定义这个方法提供的参数可以是字符串所有,对ID的数组或一个空数组?此外,我怎么确保过滤器不会做任何事情,如果该参数为所有?

解决方案

 高清self.selected_countries(参数)
  案例参数
  当全
    所有
  其他
    其中,(COUNTRY_ID:参数)
  结束
结束
 

随后的范围方法链追加,而不是取代它的查询。

所以,你可以调用这个范围内像

  Article.selected_countries([1])。selected_countries(全)
 

,你会得到所有的文章国1。

My web app is showing a list of articles. Articles can be published in different countries (i.e. Article belongs to a Country, and Country has many Articles), and in different languages (i.e. Article belongs to a Language, and Language has many Articles).

What I would like to do is to make requests that will return me articles from selected countries and in selected languages. That is, I would eventually like to make a request such as this:

Article.selected_countries.selected_languages

and get a list of articles.

Both for the countries, and for the languages, the front-end can send the following parameters:

  • "all" — that means, effectively, do not apply this filter, and return articles from all countries (or articles in all languages);
  • array of id's — that will mean, return articles only from countries with provided id's (or in languages with provided id's)
  • empty array — I guess, that is a special case of the previous option; it will mean that articles from no country (or in no language) have to be returned, so no articles will be returned

Now, what confuses me is to how to write these class methods and how to make them chainable. Rails Guides provide the following example:

class Article < ActiveRecord::Base
  def self.created_before(time)
    where("created_at < ?", time)
  end
end

if I build a class method based on this example, such as the following:

def self.selected_countries(parameter)
  ???
end

How do I define this method provided the parameter can be the string "all", an array of id's, or an empty array? Also, how do I make sure the filter doesn't do anything if the parameter is "all"?

解决方案

def self.selected_countries(parameters)
  case parameters
  when "all"
    all
  else
    where(country_id: parameters)
  end
end

Subsequent chains of scope methods append to the query, instead of replacing it.

So you could call this scope like

Article.selected_countries([1]).selected_countries("all")

and you'll get all the Articles for country 1.

这篇关于扶手:一个模型链接过滤方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆