Making those fancy advanced methods

How do you write a method thats similar to find_by_username, I want to
write a method that works dynamically like that, but I have no idea
where to begin…

Do you declare the method differently? How do you access the part of the
method header you want, i.e. username?

TIA
Luke

Take a look at method_missing, and specifically how ActiveRecord::Base
implements it in the Rails codebase.

method_missing is the method called when a method is…er…missing. :slight_smile:
For example:

def method_missing(meth)
puts “Called #{meth}, which was missing…”
raise NoMethodError
end

Playing with that in a class should get you started.

–Jeremy

On Feb 6, 2008 5:29 PM, Luke G.
[email protected] wrote:


Posted via http://www.ruby-forum.com/.


http://www.jeremymcanally.com/

My books:
Ruby in Practice

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs