有很多方法可以做到这一点,你的方法是完全有效的(虽然我个人更喜欢将类方法包装到单独的块中检查 this out ),但是随着人们在他们的模型中添加更多的业务逻辑并且盲目地遵循“瘦控制器,胖模型”的概念,模型变得一团糟。
为了避免这种混乱,引入服务对象是个好主意,在你的情况下,它会是这样的:
class AverageWeatherData
class << self
def data(collection)
new(collection).data
end
end
def initialize(collection)
@collection = collection
end
def data
@collection.reduce do |avg, post|
# reduce goes through every post, each next iteration receives in avg a value of the last line of iteration
# do something with avg and post
end
# no need for explicit return, every line of Ruby code returns it's value
# so this method would return result of the reduce
# more on reduce: http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-reduce
end
end
现在您可以通过将您的集合传递给它来直接调用这个类。但您也可以像这样代理调用:
def self.my_complicated_averaging_method
AverageWeatherData.data(@relation)
end
我鼓励您通过阅读此博客了解更多这种方法:
http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
UPD
你是对的,使用实例变量是一种搞乱对象内部的可能方法(另外它不是公共接口,将来可能会改变)。我的建议是使用方法scoped。基本上用scoped替换@relation。
检查这个例子。我使用了我自己项目中的模型来证明它确实有效
2.0.0p247 :001 > Tracking # just asking console to load this class before modifying it
# => Tracking(id: integer, action: string, cookie_id: string, ext_object_id: integer, created_at: datetime, updated_at: datetime)
2.0.0p247 :002 > class Tracking
2.0.0p247 :003?> def self.fetch_ids
2.0.0p247 :004?> scoped.map(&:id)
2.0.0p247 :005?> end
2.0.0p247 :006?> end
# => nil
2.0.0p247 :007 >
2.0.0p247 :008 > Tracking.where(id: (1..100)).fetch_ids
# Tracking Load (2.0ms) SELECT "trackings".* FROM "trackings" WHERE ("trackings"."id" BETWEEN 1 AND 100)
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
UPD
在 Rails 4 中 scoped 已弃用,因此使用 all 是正确的。
all.map(&:id)