RSS
23 Oct 2007

Be an Expert of Ruby on Rails’ Active Record!

Author: ceefour | Filed under: Books, Cool, Enterprise, News, Opinions, Praises, Rails, Reviews, Ruby, Tips

Any Ruby on Rails programmer would have touched Active Record, probably in a very early phase. Active Record deals with everything that’s related to the database of your Ruby on Rails applications, and in many ways a bit more.

 Be an Expert of Ruby on Rails’ Active Record!

Apress recently launched Pro Active Record: Databases with Ruby on Rails, which I can honestly say, is the most exhaustive, if not the only, book about Active Record I’ve ever known to date.

The book starts with a little bit of a “wow” factor, especially true if you’re new to Ruby on Rails: demonstrating the ease of use of Active Record itself. No configuration needed! Seriously, that’s not the claim of the book but it’s fact of Active Record: all you need is your database credentials and name of the table you want to work with. Everything else is almost magical.

The next sections deal with installing and configuring Active Record. I have to say sometimes (based on my own experiences) it’s not that trivial, especially on Windows. The book guides you through installing drivers/adapters for common and less popular DBMSes. OK, let’s name them: DB2, Firebird, FrontBase, MySQL, OpenBase, Oracle, PostgreSQL, SQLite, Microsoft SQL Server, Sybase, they’re all covered! I was wondering if I can connect to ODBC?

The second chapter deals with the “simple” things, namely, how to do SELECT, UPDATE, DELETE, INSERT, or what you cool guys usually call “CRUD”, the Active Record-way. This information is usually taken for granted, but it’s explained quite deeply in this book.

The next chapter guides you to design your tables so they work smoothly with Active Record. Although you can use legacy designs just fine with Active Record, your life will be much, much easier if you follow Active Record conventions. This book shows you just how, and in many cases, why.

Chapter 4 shows you how to use core Active Record features, including triggers and validations. If you’re asking that question, then the answer is yes, it’s almost completely overlapping with native but proprietary DBMS functionality such as PL/SQL, DBMS triggers, and constraints. The book doesn’t seem to explain much of this holy war, but I encourage you to ask Google why stored procedures are evil.

Chapter 5 gives you a view of Active Record “bonus features”. I think the chapter title is a misnomer because these features are not just bonus, but one of the greatest strengths of Active Record (compared to other ORM). Things like nested sets, lists, observers, aggregations, and little bit of extending Active Record: these are things that you’ll do, and use, daily — not something you try to avoid. They make your life as a DB integrator easier, not the other way around.

You want to make sure your app works fine, chapter 6 covers unit testing very thoroughly, down to the descriptions of errors (i.e. Active Record Exception objects) you may encounter.

If you already had a previous database, as most of us are, no worries, chapter 7 will guide you how to work with them. This is one of the most useful chapters in this book, because there is less information on the Internet on this topic, but it’s very common that everybody is bound to meet this problem. Come on, who actually learned Active Record before hearing the word DBMS?

The last chapter talks about issues that you’ll encounter in the “real world”, including alternatives and related enhancements to Active Record. What follows is the appendix, which is a really useful quick reference.

The book is really, really good. Full of core information, and related information that aren’t readily available even after asking Mr. Google a hundred times. Despite being written by 3 different developers: Kevin Marshall, Chad Pytel, and Jon Yurek; the entire book is nicely structured and feels cohesive. It will save you lots of time in times of frustration, you can look up the information much quickly here than does a search engine.

Interested already? You can get more information about Pro Active Record book on Amazon, and buy it there too of course. It’s also available on Apress official site.

Personal minor gripe, not a flaw, is there wasn’t a (detachable) cheatsheet. A poster-sized, deluxe exclusive cheatsheet would be a tremendous killer plus to this book. I really think Apress should make this kind of thing mandatory in the future, not only regular Appendixes.

Other resources related to this book:

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

  • http://amazon.wpbloggers.com/?p=3705 amazon » Be an Expert of Ruby on Rails’ Active Record!

    [...] Read the rest of this great post here [...]

  • http://book.wpbloggers.com/?p=6023 book » Be an Expert of Ruby on Rails’ Active Record!

    [...] Read the rest of this great post here [...]

  • http://adityakircon.blogsome.com adit

    what if some guy with OLD application & and legacy database try to migrate to ruby on rails

    you’re lucky if that legacy database structure meets active record default settings if not, you must write configurations

    class Food “dodol_id”
    end

    class Dodol “food_id”
    end

    of course you can change database structure, but if the apps already live, we must create script to migrate the database, and for me job like that is a pain

    is problem like above covered by that book ?

    *sorry for my poor english

  • http://adityakircon.blogsome.com adit

    hmm, i try to put rails code at the latest post, but it seems missing, maybe blocked by blog spaminator ?

  • http://hendy.gauldong.net/ Hendy Irawan

    Yep, I’ve been there … everyday :-)

    In fact Pro Active Record has one whole chapter devoted to dealing with legacy databases, including extras to help you in that… i.e. plugins for composite primary keys, etc.

  • http://hendy.gauldong.net/ Hendy Irawan

    adit:

    About the code, no it’s not because spam filtering just the standard WordPress text processing… Putting the code inside < PRE > tags will help.

  • http://ariekusumaatmaja.wordpress.com arie

    @adit, it’s not a pain, you’re just not getting used to it yet, once you’ve clicked, you’re riding it on a right track ;-) just do script/generate migration to generate another *correct* migration one so that script will be correct.

    in bahasa indonesia,
    sebetulnya tidak susah kok dit, mungkin cuma belum biasa saja, jadi kalau sudah terbiasa, saya yakin adit pasti akan mudah mengendarainya dengan cepat. adit tinggal melakukan script/generate migration untuk membuat satu migration yang betul sehingga skrip tsb menjadi benar.

    Kalau dilihat dari besarnya kok terlihat lebih besar, iya, tetapi adit konsisten dengan code tsb, sebetulnya bisa juga kita main “dirty hack” dengan permainan rake db:migrate VERSION=0 dan dibolak-balik juga bisa dan kurang/filenya sedikit saja, tetapi saya lebih merekomendasikan cara yang pertama diatas. btw ini terjemahan bahasa indonesia nya puanjaaang banget yak huahahaha jadi tu bule bule kalo ada yg mbaca ini pasti ngeliatnya wah kalo ngomong pake bahasa indonesia panjang panjang euy hihihi :-D