在 Ruby on Rails 中查找后调用模型中的方法 [英] Call a method in model after find in Ruby on Rails

查看:11
本文介绍了在 Ruby on Rails 中查找后调用模型中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在使用 find 后从模型中调用方法.

I would like to know if it is possible to call a method from a model after using find.

类似于after_save,但after_find.

谢谢,加布里埃尔.

推荐答案

对于 Rails >= 3,请参阅 @nothing-special-here

For Rails >= 3, see the answer from @nothing-special-here

有.不过,与 after_initialize 一起,after_find 是一个特例.你必须定义方法,after_find :some_method 是不够的.不过,这应该可行:

There is. Along with after_initialize, after_find is a special case, though. You have to define the method, after_find :some_method isn't enough. This should work, though:

class Post < ActiveRecord::Base
  def after_find
    # do something here
  end
end

您可以在API中阅读更多相关信息.

You can read more about it in the API.

这篇关于在 Ruby on Rails 中查找后调用模型中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆