Leo*_*eon 17 activerecord ruby-on-rails
我想为特定模型的所有集合添加一个方法.假设我想将该方法添加my_complicated_averaging_method到WeatherData集合中:
WeatherData.all.limit(3).my_complicated_averaging_method()
Station.first.weatherdata.my_complicated_averaging_method()
Run Code Online (Sandbox Code Playgroud)
做这个的最好方式是什么?目前,我找到的唯一方法是这样的:
class WeatherData < ActiveRecord::Base
def self.my_complicated_averaging_method
weighted_average = 0
@relation.each do |post|
# do something complicated
# weighted_average =
end
return weighted_average
end
end
Run Code Online (Sandbox Code Playgroud)
这是将方法添加到集合的好方法吗?有没有更好/支持的方式来做到这一点?
Edg*_*ons 12
有很多方法如何做到这一点,而你的是完全有效的(虽然我个人更喜欢包装类的方法为单独的块检查的这出),但随着人们正在添加更多的业务逻辑,以他们的模型和盲从"瘦控制器,胖模型"概念,模型变成完全混乱.
为了避免这种混乱,引入服务对象是一个好主意,在你的情况下,它将是这样的:
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
Run Code Online (Sandbox Code Playgroud)
现在,您可以通过将您的集合传递给它来直接调用此类.但您也可以像这样代理呼叫:
def self.my_complicated_averaging_method
AverageWeatherData.data(@relation)
end
Run Code Online (Sandbox Code Playgroud)
我鼓励您通过阅读此博客了解更多此方法:http: //blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
UPD
你是正确的使用实例变量是一种可能的方式搞乱对象内部(加上它不是一个公共接口,它可能会在未来发生变化).我的建议是使用方法scoped.基本上替换@relation为scoped.
检查此示例.我使用了自己项目中的模型来证明它确实有效
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]
Run Code Online (Sandbox Code Playgroud)
UPD
在Rails 4 scoped中已弃用,因此使用正确all.
all.map(&:id)
Run Code Online (Sandbox Code Playgroud)
在 Rails >= 4 上,您可以使用where(nil)代替scoped
class Foo < ActiveRecord::Base
def self.bar
where(nil).pluck(:id)
end
end
Foo.where(id: [1, 2, 3]).order(:id).bar
Run Code Online (Sandbox Code Playgroud)
此外,您可以使用#scope,例如:
class Foo < ActiveRecord::Base
scope :bar, -> {where(nil).pluck(:id)}
end
Run Code Online (Sandbox Code Playgroud)
最后,您可以编写如下代码 Foo.all.bar
| 归档时间: |
|
| 查看次数: |
10327 次 |
| 最近记录: |