当从数据库中提取并调用对象时,我需要一些可以触发方法的东西。因此,例如,假设我有 Apple 类(class)。我执行查询:
Apple.where(:name => "Delicious").first
返回
#<Apple id: 2, blah, blah, blah>
我需要一种方法(回调或其他)来调用方法,以便我可以跟踪从数据库中提取项目的次数。任何人都可以建议这样做的方法吗?我正在使用 MongoDB 和 MongoMapper。
最佳答案
有一个 after_find从数据库加载对象后调用的回调。所以:
after_find :update_found_count
def found_times
increment!(:found_count)
end
关于ruby-on-rails - rails 3 : Call back for when a certain object is pulled from the database (Mongo Mapper),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8969539/