Ruby on rails 4 将范围写入rails 4.0模型
我正在使用rails 4.0开发应用程序。我的博客控制器中有此代码Ruby on rails 4 将范围写入rails 4.0模型,ruby-on-rails-4,activerecord,Ruby On Rails 4,Activerecord,我正在使用rails 4.0开发应用程序。我的博客控制器中有此代码 @published = params[:published] @blogs = Blog.published_blogs.where('published >= ? AND published <= ?', (params[:published].to_date).beginning_of_month, (params[:published].to_date).end_of_month) 然后把我的控制器
@published = params[:published]
@blogs = Blog.published_blogs.where('published >= ? AND published <= ?', (params[:published].to_date).beginning_of_month, (params[:published].to_date).end_of_month)
然后把我的控制器换成了
@blogs = Blog.published_blogs.by_published(params[:published])
在Rails 4中,作用域再次发生了变化——这就是为什么我对使用它们感到不安的原因:
scope :by_published, ->(published) { where('published >= ? AND published <= ?', (published).to_date.beginning_of_month ,(published).to_date.end_of_month if published.present?)}
scope :by_published, ->(published) { where('published >= ? AND published <= ?', (published).to_date.beginning_of_month ,(published).to_date.end_of_month if published.present?)}
class Article < ActiveRecord::Base
scope :created_before, ->(time) { where("created_at < ?", time) }
end
class Article < ActiveRecord::Base
def self.created_before(time)
where("created_at < ?", time)
end
end
category.articles.created_before(time)