Ruby on rails 从其他模型继承的rails 3作用域

Ruby on rails 从其他模型继承的rails 3作用域,ruby-on-rails,ruby,ruby-on-rails-3,scopes,Ruby On Rails,Ruby,Ruby On Rails 3,Scopes,有没有办法将一个模型作用域用作另一个模型作用域的一部分 范例 class DocumentVersion < ActiveRecord::Base belongs_to :document scope :are_latest, lambda{ where(latest: true)} end class Document < ActiveRecord::Base has_many :document_versions scope :are_latest, lambd

有没有办法将一个模型作用域用作另一个模型作用域的一部分

范例

class DocumentVersion < ActiveRecord::Base
  belongs_to :document
  scope :are_latest, lambda{ where(latest: true)}
end

class Document < ActiveRecord::Base
  has_many :document_versions
  scope :are_latest, lambda { ...something... } # I want something that will get documents that have documnet_versions that are_latest

  # I know I can do this 
  scope :are_latest,   lambda{ joins(:document_versions).where('document_versions.latest = true') }  # this works well

  # but I would like to keep that condition its own model
  scope :are_latest, lambda { joins(:document_versions).are_latest } # this wont obviously work, but something like that 

end
class DocumentVersion
是,您可以使用它将作用域组合在一起

class Document < ActiveRecord::Base
  has_many :document_versions

  scope :are_latest, lambda { joins(:document_versions).merge(DocumentVersion.are_latest) }
end
class文档