2015-09-13 20 views
0

我想为一个类创建一个class method,继承ActiveRecord:Base。 该方法需要做的是添加基于选项的where子句,它运作良好。为ActiveRecord_Relation调用ActiveRecord类方法作为接收者

class Article < ActiveRecord::Base 

    def self.list_by_params(params={}) 
    articles = self 
    articles = articles.where(author_id: params[:author_id]) unless params[:author_id].blank? 
    articles = articles.where(category_id: params[:category_id]) unless params[:category_id].blank? 
    articles = articles.where("created_at > ?", params[:created_at].to_date) unless params[:created_at].blank? 
    articles 
    end 

end 

这段代码在调用等情况下正常工作:

articles = Article.list_by_params({author_id: 700}) 
#=> Works fine as I expected. 

articles = Article.joins(:authors).list_by_params({author_id: 700}) 
#=> Works fine as I expected. 

然而,问题是,如果我要拨打的list_by_params没有过滤参数,可以再它失去了往日的关系。例如:

articles = Article.joins(:authors).list_by_params({}) 
#=> articles is just a `Article` (not an ActiveRecord_Relation) class itself without joining :authors. 

有没有可能我犯了一个错误?

在此先感谢。

+0

定义实例方法(不是一类一)'list_by_params',我看不出有任何理由为什么这个'Article.list_by_params'不是扔NoMethod错误。 –

+0

您是否找到解决方案?如果我的回答不起作用。它有什么问题? – Albin

回答

1

你在找什么是scope

我会做这样的事情

scope :for_author, lambda { |author| where(author_id: author) unless author.blank? } 
scope :in_category, lambda { |category| where(category_id: category) unless category.blank? } 
scope :created_after, lambda { |date| where('created_at > ?', date.to_date) unless date.blank? } 

scope :list_by_params, lambda do |params| 
    for_author(params[:author_id]) 
    .in_category(params[:category_id]) 
    .created_after(params[:created_at]) 
end 

现在你可以重用你查询的组成部分。所有东西都有一个名称,读取代码变得更容易。

0

对于自我解释,我已经通过使用where(nil)解决了这些问题。

实际上,Model.scoped返回匿名作用域,但该方法自Rails版本4以来已被弃用。现在,where(nil)可以替代功能。

class Article < ActiveRecord::Base 

    def self.list_by_params(params={}) 
    articles = where(nil) # <-- HERE IS THE PART THAT I CHANGED. 
    articles = articles.where(author_id: params[:author_id]) unless params[:author_id].blank? 
    articles = articles.where(category_id: params[:category_id]) unless params[:category_id].blank? 
    articles = articles.where("created_at > ?", params[:created_at].to_date) unless params[:created_at].blank? 
    articles 
    end 

end 
相关问题