Topic: OOP with RUBY

Hi ...

I am a newb in ruby world. Currently, I am learning about RoR ... and i had questions about the oop with Ruby.

Yesterday I read a tutorial about using ActiveRecord. I read it from
I found the code

  class Person < ActiveRecord::Base
      def validate
        errors.add_on_empty %w( first_name last_name )
        errors.add("phone_number", "has invalid format") unless phone_number =~ /[0-9]*/

      def validate_on_create # is only run the first time a new object is saved
        unless valid_discount?(membership_discount)
          errors.add("membership_discount", "has expired")

      def validate_on_update
        errors.add_to_base("No changes have occurred") if unchanged_attributes?

My question is ...
validate, validates_presence_of, etc are the methods belong to another class (ActiveRecord::Validations). How come the ActiveRecord::Base has access on such methods ? I dont understand about that !

Is Base inherited from Validations ?

Re: OOP with RUBY

ActiveRecord::Validations is a module, not a class. Modules are a group of methods, and/or potentially a few other things, that can be mixed into classes. For a better explanation, refer to

I took a quick look at the active record gem and it does look like mixins are used.

ActiveRecord::Base.class_eval do
  include ActiveRecord::Validations
  include ActiveRecord::Locking::Optimistic
  include ActiveRecord::Locking::Pessimistic
  include ActiveRecord::Callbacks
  include ActiveRecord::Observing
  include ActiveRecord::Timestamp
  include ActiveRecord::Associations
  include ActiveRecord::Aggregations
  include ActiveRecord::Transactions
  include ActiveRecord::Reflection
  include ActiveRecord::Acts::Tree
  include ActiveRecord::Acts::List
  include ActiveRecord::Acts::NestedSet
  include ActiveRecord::Calculations
  include ActiveRecord::XmlSerialization
  include ActiveRecord::AttributeMethods

Vincent Woo Ruby on Rails Blog

Re: OOP with RUBY

Okay .. I will read the modules introduction smile It sounds new to me ...
Is the Module equal with Package in Java ?

I just not get used with the Ruby syntax (since i am new .. have just in ruby world for about 1 week .. lol)...

Also, would you explain to me on how to know all in ActiveRecord::Base, ActiveRecord::Migration, etc methods  from API Documentation ?
(I search the ActiveRecord::Migration.create_table but it goes to Modules ActiveRecord::ConnectionAdapters::SchemaStatements / Class
ActiveRecord::ConnectionAdapters::FrontBaseAdapter )

Tx a lot for explaining to me ... smile

Re: OOP with RUBY

I don't really know java, so I don't know if modules compare to packages. Modules are used in a couple of differnt ways. You can use them to namespace:

module ActiveRecord
  class Base

  class Migration

And also to group common functionality as you've seen with validations. Modules are also used as a solution to multiple inheritance. Ruby allows you to inherit from only one class. But you can mix in as many modules as you want. Gives the benefits of multiple inheritance without the headaches.

The best way to learn ruby/rails is to get your hands dirty. I highly recommend Agile Web Development With Rails and Programming Ruby (the pickaxe) in that order to learn rails and to then delve deeper into ruby. Once you've walked through a couple of tutorials, like the one in the Agile book, start building a simple app and ask questions.

Re: OOP with RUBY

Sorry, I'm not that familiar with Java. I assume they're similar in terms of namespacing.

It seems that if you call an undefined method on the migration class, such as create_table, it'll try sending the method call to the object responsible for talking to the database.

def method_missing(method, *arguments, &block)
    # ...
    ActiveRecord::Base.connection.send(method, *arguments, &block)

In my case, ActiveRecord::Base.connection is an instance of the class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter which inherits from the ActiveRecord::ConnectionAdapters::AbstractAdapter class which mixes in the SchemaStatements module.

I don't think that would be helpful to learn though. Its not really worth grepping around the source files like this unless you want to see how Rails work behind the scenes. My advice is to not worry about where methods belong. Continue searching for methods names in the api and just verifying if the first level namespaces look correct eg. ActiveRecord or ActiveController. Yes, it's a pain but you'll start memorizing the api in no time.

The best way to learn is to do/create. Try obtaining one of the many Rails books available and read up on chapters on a need to know basis as you work on some sample projects. The pragmatic programmer's rails book has been a great companion. There's also the sitepoint book (an ad is in upper right corner of this forum).

There is a WIP book at too.

Vincent Woo Ruby on Rails Blog

Re: OOP with RUBY

Yep, I start learning ror with Agile Web Dev With Ruby on Rails (still on building depot application )... and I also tried to learn others that related by reading the api documentation. But while reading it, I got confused since ruby behave a bit different with java documentation .

thank you for your recommendation smile