Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Rails 3.0′s ActiveModel: How To Give Ruby Classes Some ActiveRecord Magic

By Peter Cooper / January 13, 2010

activemodel.gif One of the biggest benefits of bringing Merb developer Yehuda Katz on board to work on Rails 3.0 has been his relentless pursuit of extracting out all of Rails' magical abilities from their monolithic encasings and into separate, manageable chunks. A case in point is ActiveModel, a new library that provides the model related parts of ActiveRecord but without the database requirements.

Get Rails-like Model Behavior on Any Ruby Class

In extracting the model-building parts of ActiveRecord, ActiveModel makes it possible to add model-like behavior to any Ruby class, with no Rails or databases required. In his latest blog post, ActiveModel: Make Any Ruby Object Feel Like ActiveRecord, Yehuda shows off how to get Rails-style models with validations, serialization, callbacks, dirty tracking, internationalization, attributes, observers and all the other Rails goodness.

Example Code

I've taken Yehuda's main example of using ActiveModel on a non Rails class and extended it with some code that actually uses the model:

require 'active_model'

class Person
  include ActiveModel::Validations

  validates_presence_of :first_name, :last_name

  attr_accessor :first_name, :last_name
  def initialize(first_name, last_name)
    @first_name, @last_name = first_name, last_name
  end
end

a = Person.new("Fred", nil)
a.valid? # => false
a.last_name = "Flintstone"
a.valid? # => true

Installing ActiveModel

If you're interested in ActiveModel and not so much in Rails 3.0, installing it is reasonably easy (though not as easy as only installing a gem just as yet):

  1. Go to or make a temporary directory
  2. git clone git://github.com/rails/rails.git
  3. cd rails
  4. rake gem
  5. gem install activesupport/pkg/activesupport-3.0.pre.gem
  6. gem install activemodel/pkg/activemodel-3.0.pre.gem

Once this is all done, the code example above will work.

As an aside, if you fancy having a go with the full pre-release (a.k.a. "pre") version of Rails 3.0, check out Dr Nic's slightly out of date but otherwise useful guide.

Comments

  1. SoftMind says:

    Hi,

    Looking forward to read such more articles of Rails 3. Your special ability as an author of Ruby book adds lots of charm in reading your blogs.

  2. Peter Cooper says:

    Thanks!

    Just as an added note.. be careful if you install these gems. I don't /think/ the ActiveSupport gem should clash with older versions but.. depending on how you're using it, it might. If other things fry, just remove the 3.0-pre version and you're back to where you were.

  3. Jamie Macey says:

    Yehuda put out significantly more recent (and easier) instructions for Rails 3.0.pre 2 weeks ago: http://yehudakatz.com/2009/12/31/spinning-up-a-new-rails-app/

  4. Stephen Celis says:

    Yehuda Katz recently posted about spinning up a new Rails 3.0 app here:

    http://yehudakatz.com/2009/12/31/spinning-up-a-new-rails-app

    It should provide more up-to-date instructions than Dr. Nic's.

  5. Daniel Berger says:

    @Peter, isn't vendoring in Rails itself within Rails apps now standard practice?

  6. Konstantin Haase says:

    Also, you can use a separate gemset with rvm, to avoid messing with your dev env.

  7. Andrew Grimm says:

    OT: Where do you get the pictures you use for blog posts, and is the woman in this photo ok? (It looks like the weights are about to go behind her)

  8. Rahmal Conda says:

    I've been brainstorming about how to include ActiveRecord-like validations and more recent active_support logic into a library I need to be compatible with earlier versions of rails. This may be the answer I was looking for. Thanks Peter! This is why I come to this RubyInside first!

  9. Peter Cooper says:

    @Andrew: Usually I make them myself or use legally sourced photos (iStockPhoto, Flickr Creative Commons, logos, etc). Sometimes, though, I just Photoshop images I don't really have any rights to, such as this one, and pray. Don't worry - she's fine, I'm sure. You can blame Photoshop's "content aware resizing" for the seemingly forthcoming disaster in the shot ;-)

    This wasn't one of my best choices, to be fair. Since ActiveModel has no logo I wanted to go with something mildly amusing on the topic of a "model" doing something "active." First I had a busty chick on a Harley but thought that might attract some whining. So then I went for muscle chicks - same deal. This was a kinda last ditch "let's get it out there" image..

  10. Peter Cooper says:

    @Daniel: Common practice, but not the standard practice, I believe.

    @Stephen & @Konstantin: Awesome suggestions.

  11. Jim says:

    Just a minor note...Ezra was the creator of Merb, not Yehuda.

  12. Peter Cooper says:

    @Jim: I'd say that's quite a key point actually.. thanks for the correction!

  13. Pratik says:

    To give credits where it's due, most of the ActiveModel work was done by Josh Peek and some by me and Rick Olson, and had been in making long before the merge :)

  14. Jay Godse says:

    Rails validations are a great way to codify some of the intentions of the designer of the domain model. That leads to better code overall.

    Kudos to Yehuda for making them available to all Ruby objects.

  15. Nick Treadway says:

    I was just thinking wouldn't it be nice to use ActiveModel in a StaticMatic like project. Very nice.

Other Posts to Enjoy

Twitter Mentions