This post originated from an RSS feed registered with Ruby Buzz
by Guy Naor.
Original Post: Small Update for the method_missing() Post
Feed Title: Famundo - The Dev Blog
Feed URL: http://devblog.famundo.com/xml/rss/feed.xml
Feed Description: A blog describing the development and related technologies involved in creating famundo.com - a family management sytem written using Ruby On Rails and postgres
A few days ago I posted a ruby trick with method_missing() to make a child object appear as part of the parent object. Well, I found out I can improve it a bit, to make sure we first check the method_missing() of the parent class, and only then the one of the child class.
The correct code is:
def method_missing(method_id, *args, &block)
begin
super
rescue NoMethodError => e
raise if !family_setting
begin
family_setting.send method_id, *args
rescue
raise e
end
end
end
The reason we need this change is to prevent us from calling method_missing() based methods in the child before we try the parent. A good example in rails would be all the find_by_xxx functions which will go to the child instead of the parent.