Archive for Ruby

Book Review, “Pro Active Record”

Published by Apress
By Kevin Marshall, Chad Pytel, Jon Yurek
Book Info
Sample Chapter: Ch. 01 - Introducing Active Record
Table of Contents

Years ago when I was in PHP Land (now I travel quite a bit more! haha), I strugged for months with how to write a good ORM . It was tough, because I wanted to abstract the "boring logic" of retrieving records from a database without writing SQL but still remain flexible enough. I never really came up with a good model. I used the DAO from "extreme php" library which I think was a knock off from java. It was ok, but I still didn't feel like I had "arrived".

When I discovered Ruby on Rails, I found ActiveRecord. Ahh HA! Finally, this is what I was looking for. At first I thought it was part of Rails, but its not. Its a standalone library and you can use it with straight up ruby scripts.

I got a review copy of "Pro Active Record" some time ago and read it some when I got it, then some later, and now I am going to officially write up a review!

If you do anything with Active Record, get this book. The things that are briefly mentioned in most Rails books are described in detail in this book.

Chapter 1 - Introducing Active Record

Most of the time, the first chapters of a book are boring to me. I don't need another "History of the Internet" or how "HTML was developed" ... blah blah. But this one, the story is only 1 page. And it actually has some introductory scripts on using Active Record, so you can see right away how it works. It also explains the benefits of MVC and why ORMs are good. Some people still don't get it!

Chapter 2 - Active Record and SQL

This chapter helps you translate the "sql in your head" to how to write it with Active Record. I've used Active Record so much that now I have forgotten most of my SQL, which is kind of embarrassing. :) I now find writing sql tedious and boring! I would have actually called this chapter "Demystifying Active Record" since it explains why all the dynamic finders work. You'll also find transactions and locking explained here.

Chapter 3 - Setting up Your Database

Migrations! The Awesome Thing that can turn into a nightmare for large rails projects with multiple developers.... definitely have to decide on some best practices with your team on this one. The chapter has only one thing to say about this -- assume any checked in migration has already been run by your team and the migration should not be edited and checked back in! You'll have to make another migration file with your changes.

[tip]
Nola's Note: When you make a migration, test it both UP and DOWN!! Here's what I do --
write a migratiion
rake db:migrate (go up to the version with new code)
rake db:migrate VERSION=n-1, (go to version before the latest)
rake db:migrate (back to lastest)
rake db:migrate VERSION=0 (back to blank db)
rake db:migrate (back to latest)
[/tip]

Just to be sure its all good -- even on a new database!

Chapter 4 - Core Features of Active Record

Now is the fun stuff - Callbacks. This is magic. This makes Active Record so flexible, and is one thing I could never figure out how to do with my PHP ORMs. I use call backs to set defaults for fields. If its just a straight default, then I set it in the database but if I need to make a decision, (if this field then this field..) then I can use it in a callback.

Associations - at first this is very confusing! I don't know how many times I got "has_many" and "belongs_to" mixed around in the beginning.

Validations - Awesome. I had to do some ruby code without a database and I really really really missed the validations. It took me like 5x longer than it should! Understanding all of these validation methods will make your life so much more enjoyable. I really really hate doing boring, repetitive stuff...it seems so wasteful to me.

Chapter 5 - Bonus Features

Everybody likes a bonus and this isn't even the last chapter of the book.

Java people will like the Active Record Observers -- seems a little AOP to me (aspect orienteted programming) and something I probably have neglected to use to their fullest extent.

Acting up -- Learn how to "save time" with the "acts_as" magic: List, Tree, Nested Sets. If your data needs these structures, you got it made. I can imaging how much longer it would take to write this stuff in perl or php.

Composed of - I haven't used this, but this looks like a good way to make sensible objects out of database tables. There is quite a bit of explanation and examples of this, it will come in handy.

There are a few other in depth explanations of things, such as method_missing which is how alot of the magic happens. Rock on.

Chapter 6 - Active Record Testing and Debugging

Ahh yes, Testing. My favorite subject. My friends who know how much I love testing say I am sick. I must have an inner need to PROVE I am right or something, haha.

The chapter goes into depth about using test_unit with Active Record, sadly no RSpec. But, it does go into all the error messages that Active Record throws so you can write good try/catch blocks and make very exact error messages (probably best logged for the admin rather then displayed to the user!)

Chapter 7 - Working with Legacy Schema

Here's how you work with that old database that just won't die... or that management won't let you totally redo. Active Record follows some of the principles of Rails "convention over configuration" ... relying on table and column naming conventions to figure out how to build your object....but still giving you a way out if you want your tables singular and your primary id field called "myawesomeid" instead of "id"

I've used some of these things on an older database and it was possible! Not too bad if thats what you have to work with.

[soapbox]
Some people find this annoying "oh gosh! my library can't make decisions for me! OMG! That sucks" .. to that I say, "Umm ok. But if you follow these conventions then I can come into your project and know exactly what is going on" ... like with web standards, we all harp on how IE and FF do things differently, yet people want to bellyache about Active Record preferring to have plural names and id field called "id". Right.

Follow the dang convention and find something worth complaining about to complain about. :)
[/soapbox]


Chapter 8 - Active Record and The Real World

This chapter goes into depth about the library and encourages you to go read the Active Record code. Always a good idea to know what it is you are using :) I've actually learned ruby better by reading source code. The chapter walks you through basic structure of the files. Very cool.

[soapbox]
I used to work at a place that didn't like any "outside code" because they were afraid "OMG ... it will send our passwords to Russia!" ... ok, well I am not an idiot. I read over any code that I use that I didn't write. I look at the tests to see if I am using it right. I even RUN the tests so I can be sure its working as advertised.
[/soapbox]

Alternatives to Active Record - with EXAMPLES! If something about Active Record doesn't set too well with you, take a look at the alternatives. Sometimes I look at the alternatives and decide that the first wasn't so bad after all. You'll find examples of DBI, Og, ActiveRelation.

Finally a section on Q and A finishes up this book. The Appendix has a complete reference of ActiveRecord methods to make this book a well rounded reference, tips, documentation and very handy to have at your desk!

Comments

Is Rails suffering the same problems as PHP?

I know that mentioning Rails and PHP in the same sentence is taboo and can incite many flame wars. But hear me out -- Some say one of the "downfalls" of PHP is that its so easy to write applications and just about anybody can write a PHP if they put their mind to it and learn the syntax. As a result, PHP Applications have a long history of badly written, insecure code and has really been shunned by many advanced developers (aside from many poorly named methods, I think PHP is alright!).

In recent months I've been recruiting for a Rails Developer at my company, in Chicago. I did a phone interview and the guy couldn't name me any rails sites he reads, or name the 7 rest actions, or even tell me what version(s) he uses. Now, I'm not claiming to be an expert, but I would think that even an average Rails developer would know that.

I've also seen code where the developer doesn't quite get MVC. You don't need to be validating values in your controller and adding error messages to @model.errors.add for example. You don't need to be setting up display labels in your model. You don't need to be counting things in a helper method. I was thinking, why is it so hard? Is it because in mostlanguages we have had free reign to interpret MVC however we pleased? I know I spent many years in PHP trying to develop a DAO. I made an object that returned a row of data, with methods to access the fields. But what about a collection? what is that? I experimented with an array of objects but I felt that was too wasteful. Iexperimented with a DaoSet that was made to hold a collection -- complete with iterators! I even struggled with MVC and knowing what went where...when I saw Rails it was like someone who met their true love -- this is it!

After experiencing Rails, I realized hey I can do some of this in PHP! I wrote a simple MVC framework in PHP. I called it StupidlyEasyMVC Framework. I was able to use it at work (at the time, I was still at a PHP job. Some say I talk about that time in my life as if it were a prison sentence haha). SO I was able to apply things I learned from Rails, into an existing structure at work. Just like the guy from CDBaby -- I couldn't drop my existing PHP site and just start converting to Rails, but by starting to gradually switch from what I had to an MVC structure, it made my code cleaner and easier to work with.

So as I look for advanced rails developers, I find they are far and few between. Either they are hotshot consultants or working on their own startup. What makes a rails programmer "get it" and really work with the framework instead of against it - still trying to write as they always have?

Here are some things you can do if you feel you are still writing PHP or .NET code in rails..

Get a project. You can read and dabble all you want, but if you don't have a project, you won't be forced to learn. Along with that, you need a timeline to force you to keep at it!

Watch all the Peepcodes! especially the REST and rspec episodes. I've search hard and long enough for answers, to find comprehensive training in ONE spot, in such an easy to learn format is awesome. I constantly talk about peepcode and I won't work with anyone who hasn't watch them.

Peepcode Code Review - their first book, full of helpful hints and gotchas that will really help you move beyond a beginning developer

Ruby Inside Blog - www.rubyinside.com

Jay Fields - http://blog.jayfields.com/

Obie Fernandez - obiefernandez.com

Books:
Agile Web Development with Rails - If you are doing Rails and don't have this book, get it! It is the bible of rails! I don't care if you have the first edition already, this will save you time!
The Ruby Way by Hal Fulton - because if you don't know ruby, you will never be a good rails developer.
The Rails Way by Obie Fernandez - its good because I tech edited it before publication! It is a comprehensive instructional and reference book -- all in one. I cannot wait to get my hands on a paper copy of this to keep at home and one for work. (just got one this week...I'm ecstatic)

Well this has been mostly a rant but hopefully a help resource to move into more advanced development of rails. If you have other insights into this problem or suggestions for how to improve, please leave a comment!

Comments (3)

Beautiful Ruby: Learning the -isms

I did PHP for 6 years.... sometimes its hard not to do PHP in ruby...

Today i was testing a url to see if it contains a certain string:

RUBY:
  1. response = Net::HTTP.get_response(URI.parse("http://myawesomesite")
  2. if response.body.match("my awesome text") == nil  then
  3.       return false
  4. end

and remember to do:

RUBY:
  1. response = Net::HTTP.get_response(URI.parse("http://myawesomesite")
  2. return "false" if response.body.match("my awesome text").nil?

Another thing I was working on was I had to do a multiple if statement

I will simplify it for this example:

RUBY:
  1. color = red
  2.   if (color == "red") or (color =="green") or (color == "yellow") then
  3.     print "its valid)
  4. end

In PHP I might have actually made the values into an array, and then did in_array ... but in reading the code, you see below, if in the valid values, print something. I don't want to see array!

This is more elegant:

RUBY:
  1. color = red
  2. valid_values = ["red","green","yellow"]
  3. puts "its valid" if valid_values.include? color

This seems to only work for values you'd do == on, not ===

See... programming in ruby just makes me grin :)

Comments (3)

Nifty Rails Stuff

I've been kinda on a rails spree lately, I guess I've had some free time and also redoing a rails 1.1.6 project (and was noob then and no clue of what I was doing) and updating it to use REST.

I got Geoffrey Grosenbach PeepCode's REST Tutorial, which is very awesome and well worth the 9 bucks. Its over an hour long! I also got the RJS Tutorial which is also awesome. I can't wait till payday to get the $40 pack so I can get some more! There's just something about SEEING something work that makes it stick sometimes.

Also I spent some time on Railscasts which features a ton of short demos on rails topics. Very cool. In particular i liked Testing Without Fixtures which is cool because sometimes fixtures are a PITA!

When watching one of the PeepCode screencasts I saw him using "autotest" .. a cool toy which I must have too! I searched for it and found this posting on his blog. Now I'm autotesting all the time :) It basically monitors your rails site and runs the test everytime you change your code.

I've been using e-TextEditor and slowly learning the shortcuts. I love the font that was on PeepCode's screen cast and found a link on the PeepCode FAQ where I can download it. For Free! And it mentioned the name of the theme used in Textmate -- which happened to be the one I had found in e-Text-Editor and was using. No wonder I liked it! I also used this font in my Putty settings and changed the awful default colors (esp that dark blue! hard to see!). e-TextEditor is in Beta but its in great shape and i was so happy to see a "close all tabs" feature put into the latest build. I'll be ecstatic when they make a linux version.

Comments

So long PHP and HELLO NEW YORK!

PHP

Not!

Well I'm not saying that, because last time I did and I jinxed myself and had to come running back after only 3 months of my perl job that ended abruptly. Last monday, I started a new job as Perl Developer with a hint of Ruby. They are a bit reluctant to do ruby since most of the people there don't know it... but thats ok. I understand that fear. So, I'll be doing mostly perl and ruby when I can justify it :) I am not sure when i will do PHP next .. I will try to keep up with PHP and not go into a total PHP Coma.

Its a bit strange to be back in a huge company with their strict IT policies, procedures, forms, blah blah. But kinda nice in a way to have all that structure back. The past few jobs have been in tiny companies and i was basically IT, Helpdesk, Admin and programmer... and I was not beyond running to staples for office supplies.

Ruby in NY!

I went to NY for the Gotham City Ruby Conference, it was a blast. The topics were really interested and covered a wide variety of applications -- Ruby is NOT JUST for Rails! Rails is cool.. but its ruby that gets me all excited..

Here's some posts with notes about the conference from Bryan Helmkamp's blog:

Yeah, I'm kinda lame.. posting his notes instead of my own :)

Thanks to all the GoRuCo Sponsors: Google, Indaba Music, StreetEasy, Obtiva and Engine Yard.

Looking forward to next year, I absolutely loved New York and can't wait to go back!

Comments

Syntax .. Smintax ..

Last week I was called upon to write some Perl - something I haven't done since last fall. It was funny how as I was working on it, it started to come back to me. So I thought it'd be fun to compare my favorite languages a little bit:

Looks at how arrays are defined and the way I like to loop through them:

PERL:
  1. @books = ('Learning Perl', 'Advanced Perl Programming', 'Perl Best Practices');
  2.  
  3. foreach $book(@books) {
  4.   print "* $book\n";
  5. }

Foreach is actually an alias for "For" and some prefer that in this use because it makes it more readable. It also looks like php...

Now, for the language that has consumed my time the past 6 months...

PHP:
  1. $books = array('Pro PHP Security','PHP Cookbook','Pro PHP XML and Web Services');
  2.  
  3. foreach($books as $book) {
  4.   print "* $book\n";
  5. }

And.... here's ruby:

RUBY:
  1. books = ['Programming Ruby','Ruby Cookbook','Mr. Neighborly\'s Humble Little Ruby Book']
  2.  
  3. for book in books
  4.   print "* #{book}\n"
  5. end

I never actually used the for loop like that with an array, I usually use this version which is what the parser converts it to anyways:

RUBY:
  1. books.each do |book|
  2.   print "* #{book}\n"
  3. end

Fun stuff :)

Comments (1)

Ruby Weekend

As some people plan vacations, I'm planning a Ruby weekend.

Today Friday, I'm going to get my day job projects worked on so I don't have to do any on the weekend, do any errands and other things today and clear the weekend! I will be working on a site for my mother's new Bakery/Coffeeshop! Save for going to the gym every day for a while, church on sunday and visiting with family on monday, I'm going to see how much I can get done in 3 days.

I'm always excited to see company's blogging about their progress, especially with development. I happened to see this post on the ROR Blog about Revolution Health. And here is their blog. Has some interesting things about their progress with Rails. Also, a friend of mine, Peter Harkins is starting his own company and is blogging about how he is using rails -- good tips to be had there also.

In addition to companies, I also like to follow independent consultants like Obie Fernandez, who is starting his own consulting company and is posting some interesting tips along the way.

Its companies and people like this that inspire me to work on extra projects, and I'll be keeping my eye open this weekend for any neat trick I discover on my "ruby vacation."

Comments

Compiling Ruby 1.8.5 on Ubuntu or I’m not afraid of make!

If there is one thing I learned from perl -- is not to be afraid of make! I installed countless perl modules... and when I first started using Linux heavily, I had to compile some apps! Yikes. But not afraid no more...

I go to the RubyOnRails.com download page to see what the latest version of Ruby is, so I can install it on my Ubuntu VM (downloaded from vmware list of virtual appliances, and running with VMWare player). I was pretty sure it was 1.8.4 but I know there is one version that won't work... and whoa, its version 1.8.5 .. I look in my ubuntu package manager .. and they only have 1.8.4 - always up for a challenge i thought I'd try to compile it myself.

I had to install "build-essential" to get all the goodies "ld" in particular, that it complained about the first time I ran make. Here's what I did:

Downloaded source from www.rubyonrails.com/down

sudo apt-get install build-essential

tar xvfzp and CD into that dir

sudo ./configure

sudo make

sudo make test

sudo make install

(don't forget the sudo! or you may get strange errors!)

Then, to double check and revel in your accomplishment do:

ruby -v

Ahh! wham-bam and you are on the latest and greatest release version! Also while I was at it, I peeked at the source, poked around a bit and was thankful there are a great deal of people smarter than I who wrote this wonderful language.

Comments

Feb Ruby Meeting Report - Capistrano and Starfish

I came to the meeting knowing a bit about capistrano and nothing about starfish and left with a firms grasp of basic concepts of both!
In short:

  • Capistrano - A tool for deploying actions on multiple servers. Not necessarily for Rails and you don't need Ruby on the deployment servers! Presented by Michael H Buselli
  • Starfish - Distributed programming in Ruby. Presented by Peter Chan

In Long(er):

Capistrano
Like Rails, this tool relies on convention over configuration and makes some assumptions about your environment such as Rails, Subversion, Apache 1.x and FastCGI. Of course you can override some of these assumptions and even use it with PHP and CVS (yikes). Future versions will be completely separate from Rails. I know people who stiffen at any mention of Rails, but really.. this is how tools are born, out of a need. This one so happened to be a need by Rails developers and thus it makes sense it would be naturally easier to use to deploy a rails site.
Commands
The basic commands are run, sudo (run as root), put, delete, render (returns output from erb template) and get. In addition you can add your own commands.

Tasks:
You group commands similar to a batch file or shell script. And interesting thing is if your task is called "say_hello" ... you can also have a task "before_say_hello" and "after_say_hello" that will run before and after respectively. This might be useful for making "changes" to some of the standard tasks that will do any preparation or cleanup without having to hack the code. The question was asked if you could call "before_before_say_hello" and yes, recursive calls that that do work..though I think it could get pretty confusing!

Roles:
Machines are grouped by roles, such as "web", "db" and you can have multiple machines in those roles. The db role is unique in that you specify one as primary, because thats where the migrations are run (then I'm guessing that the database is just copied to the other database servers?).
Putting them together:
You can specify on the tasks which machine role it is to be used for such as:

task :say_hello_to_webservers, :roles => :web do
run 'echo "hello world" '
end

Anyways thats the basics as I understood. Please correct me if I am off base.
Link to presentation and resources: http://www.cosinewave.net/ruby/cap

Here's a blog posting that describes how to use Capistrano with Perl or PHP which I bookmarked some time ago, it may be a little out of date but probably has some good information still.
---

Then we had a brief moment of fun as we watched this (which is no joke!)

Erlang - http://tinyurl.com/ytgp27

---

Starfish
This was interesting as I have never done distributed programming or had a need too, but I'm always wondering how things work. The presenter said that he thinks this is one of Google's secret weapons in making things load faster.
The starfish file consists of two sections -- the server and the client. The server section describes the process and the client section describes the output. Once that is set, you run the starfish program and the first time you run it, it starts a client and a server. To start another process, run the starfish command again and this time it sees there's a server already started and then just starts up another client.
ex:
starfish find_primes.rb #starts server, client
starfish find_primes.rb #sttarts another client
Pretty neat, I had to leave before the end of this talk since I have such a long commute home but I got the jist of what starfish was and know where to look if I need distributed programming in the future!

Links to his demo files: http://oaktop.com/go/starfish/

---

Live in Chicago? Join Chirb, the Chicago Ruby group. Can't make it downtown? There some individuals starting meetings in the burbs, join the mailing list for details!

Comments

Ruby can do that . . .

Peter Harkins at Push.cx wrote a bit of code in python. I've noticed Python and Ruby being very similar, so I wanted to rewrite his example code in Ruby and see how it compares. His motivation was to basically find a way to compare two objects for equality (containing the same values for its members) since the == operator doesn't function as expected.

Here's my code:

values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['h', 'c', 'd', 's']

class Card

attr_reader :value, :suit
protected :value, :suit

def initialize(value, suit)
@value, @suit = value, suit
end

def to_str
sprintf "<card: %s, %s>", @value, @suit
end

def equal( other )
(@value == other.value) && (@suit == other.suit)
end

end

This may not be the best implementation, I'm new to Ruby so there is probably a better way to do it! I created attribute readers and then set them as protected so only other instances of class "Card" can call it (don't need public access right now, so why open that door?). Constructor takes the two parameters and sets the class variables. The method to_str . . . It was my impression that if I defined a to_str method and did "puts card" it would automagically call the to_str object.. but no, it didn't work. Ok then what is the advantage over defining to_s?

When I run it

card1 = Card.new("3", "s")
puts card1.to_str
# prints <card: 3,s>

card2 = Card.new("3", "s")
puts card2.to_str
# prints <card: 3,s>
puts "is card 1 the same as card 2?"
puts card1 == card2

# result would be:
# is card 1 the same as card 2?
# false

puts "now using equal method"
puts card1.equal(card2)

# result would be:
# now using equal method
# true

BTW - printing the instance var itself renders something goofy like:
#<card:0x2815b38>

It's the object id, which is why you can't use the == operator, since each id is different

Comments (1)