【发布时间】:2012-07-08 17:17:07
【问题描述】:
我正在阅读Beginning Rails 3。这本书创建了一个项目,用户可以在其中发布文章。现在在 Article 对象中,他们创建了 3 个范围,如下所示:
scope :published, where("articles.published_at IS NOT NULL")
scope :draft, where("articles.published_at IS NULL")
scope :recent, lambda { published.where("articles.published_at > ?", 1.week.ago.to_date)}
现在最后一个 lambda 函数我可以用这个 scope 语句替换它,我得到相同的结果:
scope :recent, where("published_at > ?", 1.week.ago.to_date)
在这里使用 lambda 有什么好处?
【问题讨论】:
标签: ruby ruby-on-rails-3