【发布时间】:2012-02-26 21:43:15
【问题描述】:
给定以下 ActiveRecord 模型:
class User < ActiveRecord::Base
has_many :games
def name
"Joe"
end
def city
"Chicago"
end
end
我想检索我直接添加到 User 类的方法列表(而不是通过扩展 ActiveRecord 和/或添加关联添加的方法)。示例输出:
["name","city"]
调用 User.instance_methods(false) 返回 ActiveRecord 添加的方法:
["validate_associated_records_for_games", "games", "game_ids", "games=", "game_ids=", "after_create_or_update_associated_records_for_games", "before_save_associated_records_for_games"]
连同来自数据库列的任何模型属性。我想排除这些,只获取子类上的自定义方法。
我的目的是方法跟踪:我想跟踪我的自定义方法,同时排除 ActiveRecord 添加的方法。
有什么想法吗?
【问题讨论】:
-
你用的是什么版本的rails?
-
我正在使用 Rails 2.2.2 进行测试,但我愿意接受更新版本的解决方案。
-
在 Rails 3.2 中,属性访问器、关联访问器以及可能的验证内容都在您的模型中包含的方法中定义,这使您尝试做的事情更容易
标签: ruby activerecord metaprogramming