0

我有一个模型,其中我实现了一个返回一组记录的方法。是否可以在 Arel 中推荐它们?

class A < ActiveRecord::Base
  #associations here

  def self.mymeth
    #return a set of records based on a query
    B.select(col).joins(:cs).where(some_condition)
  end

end

class B < ActiveRecord::Base
  #associations here
end

class C < ActiveRecord::Base 
  #associations here    
end

现在我怎么能把 mymeth 引用到类似的东西

  A.joins(:mymeth).where(condition).count
4

1 回答 1

1

你不是在找范围吗?

class A < ActiveRecord::Base

  scope :myscope, lambda { joins(:b).where(column: true) }
end

然后,您可以使用以下内容调用范围:

A.mymeth.where(col: false)

您将在范围中添加的所有 SQL 条件将在调用时自动添加到您的查询中。

于 2013-07-22T13:58:50.023 回答