Ruby 使用ActiveRecord';返回单个结果的范围指令

Ruby 使用ActiveRecord';返回单个结果的范围指令,ruby,activerecord,scope,simplification,Ruby,Activerecord,Scope,Simplification,我有一个在单元测试中运行良好的类,但我觉得它可以更简单 class License < ActiveRecord::Base scope :active, lambda { where("active_from <= ?", Date.today).order("version_number DESC") } end def current_license return License.active.first end public :current_lice

我有一个在单元测试中运行良好的类,但我觉得它可以更简单

class License < ActiveRecord::Base
  scope :active, lambda {
    where("active_from <= ?", Date.today).order("version_number DESC")
  }
end

def current_license
  return License.active.first
end

public :current_license
类许可证其中(“active_from将其作为一种方法,您可以得到:

class License < ActiveRecord::Base

  def self.current_license
    return License.where("active_from <= ?", Date.today).order("version_number DESC").first
  end

end
类许可证返回许可证。其中(“活动\来自:-”)是的,这很有效,而且更简单。一个问题是,
self是需要的,还是仅仅是一个样式?self是需要的,因为这是一个类方法。真的吗?我以前没有见过。但是,更一般地说,是否真的有任何方法可以让范围查询返回单个结果?@Dave Sag,现在请检查
.limit
方法。