我想为一个类创建一个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.
有没有可能我犯了一个错误?
在此先感谢。
定义实例方法(不是一类一)'list_by_params',我看不出有任何理由为什么这个'Article.list_by_params'不是扔NoMethod错误。 –
您是否找到解决方案?如果我的回答不起作用。它有什么问题? – Albin