[rspec-users] newbie cucumber tutorial

Matt Wynne matt at mattwynne.net
Tue Dec 23 14:49:16 EST 2008


On 22 Dec 2008, at 19:09, Sarah Allen wrote:

> I realize this is off-topic for the RSpec forum and cucumber tutorial,
> but I'm hoping you'll enlighten me on this point which is, I guess,  
> more
> of a Ruby language question...
>
> My Task model is simply defined (by the generate scaffold script) as:
> class Task < ActiveRecord::Base
> end
>
> How is it that I can call Task.create! which is a method of
> ActiveRecord::Validations ?

ActiveRecord::Validations uses a trick that's used widely in rails,  
and is arguably[1] becoming a Ruby idiom, apparently.

This is the interesting bit, around line #275:

     def self.included(base) # :nodoc:
       base.extend ClassMethods

So basically that means that when the class ActiveRecord::Base  
includes the module ActiveRecord::Validations, that module extends the  
thing which included it (in this case ActiveRecord::Base) with the  
class methods defined in ActiveRecords::Validations::ClassMethods.

Does that make sense?

I highly recommend reading David A Black's 'Ruby for Rails' at this  
point in your learnings. It did me a world of good when I was  
similarly new to Ruby. It's a really good deep grounding in Ruby IMO.

[1]http://blog.jayfields.com/2006/12/ruby-instance-and-class-methods-from.html

Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com



More information about the rspec-users mailing list