Fresh Meat – First Impressions of Agile Web Development with Rails (2nd ed) and Ruby for Rails

I have bought both books recently. Maybe it is a little bit early to write a review since I am through just a few chapters in both, I have been so impressed that I had to write a (not so) short entry at least ;-)!
I have been using C++/Java/Python for years, and have been reading as much books on every possible aspect of development with these languages as I could get. In my oppinion Ruby and Rails are absolutely superior to all of these languages (and their web frameworks/related things) in this context – The Pickaxe, AWDwR and R4R cover nearly all the things you will ever need to learn the language and Rails – and not just understand the ‘how’, but also the ‘why’, learn best practices, coding and development methodologies from code style to design issues, related technologies and more.

The point is not (only) this, since you can do the same with a few Java books (although not 3, but something like 10), however you will have harder time with Python (There is a Django book on the way, and also the Turbogears guys are publishing one but neither are out yet – but no PickAxe, AFAIK) – however, the point is, based on the experiences i have had with C++/Java/Python books that neither of them are so well written/to the point/effectively explained/well built up than the Ruby ones. Simply put: The Ruby/Rails books are the best technical books i have ever read on programming and (web) development.

AWDwR 2nd ed Agile Web Development with Rails, Second Edition
I begun to develop the depot application with the first edition about a month ago, and since I was new to both Ruby and Rails, I have thought: WOW! I was about halfway through the depot chapter when i have noticed the announcement about the Second Edition a few days ago. I have purchased the pdf version immediately, and I am quite sure this purchase has to be listed in my ‘Best value for money TOP 10’ list (along with R4R ;-).

It was a very refreshing experience to code the depot application from the first edition, but as I got into Rails more and more, I have felt that there are some small gaps here and there. On the mailing list, everybody was talking about migrations (i had no clue that time about them), helpers, AJAX/RJS, REST and other stuff i was not able to find during the depot development, and though the amount of information and level of coolness was overwhelming, I still sensed there is still even more than this.

After redoing the depot application with the second edition, all these things (among others) are finally there! You get all the goodies from the very beginning (migrations, writing your own helpers, AJAX etc) so you do not have to search the Web for the newest features anymore.My overall impression was that the small annoying things are gone, the good things are even better, thus the overall experience of reading the book/following the code is even more delightful! I can’t wait for the next chapters! This upgrade definitely rocks! Well done Dave et al.

Ruby for Rails Ruby techniques for Rails developers
I have gone through just the firs three chapters so far, and though they are supposed to be introductory chapters (entitled How Ruby works, How Rails works and Ruby-informed Rails development) I could not believe how much info I got out of them. I have to say that I am a totally mega-n00b to both Ruby and Rails, but still, I have gone through the PickAxe and half of AWDwR, 1st ed, i am a regular reader of Ruby-talk and the RoR mailing lists, so on the other hand I have some basics, and still these chapters provided me a lot of new insight.

The book (or at least the first three chapters) is extremely well written, easy to grasp yet the breadth of knowledge is really impressive. It really shows the design philosophy, logics, inner working of the things rather than just providing some theory with a few examples, or being a ‘yet another Ruby/Rails book’ in any way. If you would like to find out how the things really work, and why they work that way, rather than just be a developer who can do this and that with Rails, definitely check this book out!

R4R

There was a kind of flamewar on the Ruby on Rails mailing list about the pricing of the AWDwR second edition: One group argued that they should get some discount because they own the first edition, and the other party did not agree with this standpoint. Well, personally I am definitely in the second group – I did not hesitate to buy the PDF for a moment – I can understand (but not support in any way) the arguments of the first group, but I would gladly pay for this book even $100, not $20+, regardless of the editions I own. (And just FYI, I am a full time Java developer and though I would like to get a Ruby (on Rails) job ASAP, due to different constraints RoR is and will be just my hobby for some time)

Just my 2c.

Ruby on Rails Migrations

Although migrations are a very cool feature of Ruby on Rails, they are not covered in any of the basic books on RoR i have encountered so far (Agile Web Development with Rails, Ruby for Rails Programmers).

Update: Check out my recent post: Ruby on Rails Migrations: Reloaded for an update.

Both these books are using an ‘in medias res’ style approach – they guide the reader through the essential features of Rails by building a web app from scratch. The models in the examples are creaed in SQL rather than with migrations. Let’s examine the difference on a simple example, taken from AWDwR. (Further I am assuming that you have generated a rails application, a development database for the application and the DB connection settings (database.yaml) are correct.)

The classic way: SQL DDL

Create the sql file, create.sql:

drop table if exists products;
create table products (
id           int            not null auto_increment,
title        varchar(100)   not null,
description  text           not null,
image_url    varchar(200)   not null,
price        decimal(10,2)  not null,
primary key (id)
);

After this, you can create the table with:

mysql name_of_your_DB < create.sql

You are now ready to generate your model.

Doing the same with migrations

In your rails app directory, issue the following command:

ruby script/generate migration ProductMigration

then open the file db/migrate/001_product_migration.rb and edit it. To achieve the same result as in the SQL example, the file should look like this:

class ProductMigration < ActiveRecord::Migration
def self.up create_table :products do |table|
table.column :title,
:string,
:limit => 100,
:null => false

table.column :description,
:text,
:null => false

table.column :image_url,
:string,
:limit => 200,
:null => false

table.column :price,
:float,
:null => false
end
end

def self.down
drop_table :products
end
end

Run the migration wit the following command:

rake migrate [VERSION=version_number]

And you achieved the same result as with the first method!

That's very nice, but...

Well, if the only purpose of migrations would be solely the possibility to write Ruby code instead of SQL, even this would be enough for me to go for them. However, i have to admit that this alone would be a rather feeble argument. The good news is that it is not! There is much more to migrations than writing Ruby code:

  • Migrations are DB agnostic - The 'write once, use everywhere' principle really works here!
  • You don't have to think about obscure SQL specific things anymore - let Rails handle them for you! (OK there are some really complicated things, but fortunately they are adressed by some great books like Rails Recipes, code snippets like Migrate Plus, and I believe that by the Rails team, too.)
  • You can change the database as much as you want, and the data you have already there is not affected.
  • You get very effective versioning: track changes, concurrent versions, upgrade/downgrade your schemas easily!
  • You can generate DB schemas from migrations.
  • And possibly much much more... I am a newbie too! 😉

In my oppinion, judging based on the Rails mailing list discussions, migrations are accepted more and more as the definitve way of creating, maintaining, versioning your DB models - so everybody considering serious Rails development should give them a look!

Announcing rubychallenge.com

About a month or so ago, we begun to work on a new ruby puzzle site entitled rubychallenge.com with Alex Combas.

Some people may think after reading this line: “Yet another ‘not pron’ or ‘pythonchallenge.com’ clone”. Well, i would not say we did not borrow some basic concepts from these great puzzle sites, but our final product will have not very much in common with them: There will be programming puzzles on rubychallenge.com, and the domain suffix is equal with that of the pythonchallenge site. However, the analogy stops here. Rubychallenge will offer an entirely different programming/game/puzzle experience compared to all the similar sites out there, both in terms of game concepts and mechanics, as well as entirely unique site structure/design.

We have tons of ideas in our wiki already, and right now we are fledging out which ones to implement. Alex just set up a working development environment, so after ironing out some ideas crucial to begin, we might even write some code soon 😉
We would like to come up with a presentation/demo page as soon as possible, and possibly a development blog to inform you what’s going on. At some point we would like to incorporate some beta testers, so stay tuned!

Java and Ruby (on Rails)

Being a professional Java programmer myself, i collected some links that might bridge the gap for Java programmers who would like to take a peek at Ruby:

Introductory materials

Java – Ruby integration

  • JRuby – A 1.8.2 compatible Ruby interpreter written in 100% pure Java. Charles Oliver Nutter, one of the JRuby developers in a discussion claimed that “We’re currently working to make JRuby more EJB and J2EE-friendly, so you’re certain to see more of these opportunities.”

The question from the top of every RoR/Java FAQ:

Ajax Goodies:

Not exactly java, but nice Rails+Ajax technology showoff

Canada on Rails observations

According to more people (including me), the nicest wrapup of the Canda on Rails conference can be found here: Canada on Rails, Day One and here (part 2): Canada on Rails, Day Two.

Some things i found interesting/funny:

The first speaker was (surprise, surprise!) David Heinemeier Hansson. He had an interesting slide:

IMG_0880

Alex’s comment:

“David explains in no uncertain terms that some people do shout quite loudly for features to be brought into rails core and when it makes sense to incorporate those features he does, but sometimes it really makes no sense and so he tells them a frank “no” and this sometimes seems to cause people to complain and even threaten to stop using “my free product” which elicits the response that you see in Davids presentation slide above. “

Hmm. Interesting explanation 😉 On the presentation of Amy Hoy:

Second to last was Amy Hoy, and I dont think I need to tell you that she did a great job. Her talk was entitled “Ajax with Rails” so she actually got to demonstrate some live code and even wrote some for us in real time in response to a question someone in the audience asked…

I wish i had the nerves to do this, usually i am happy if i can present my prepared slides at a conference, not write LIVE CODE 😉

For the full event description read Alex’s blog.

Another nice conference notes can be found here: At the canada on rails conference.

Getting started with Ruby on Rails

More people keep me asking about how to get started with Rails. Here is my suggestion:

  • Installation
      • If you do not have Ruby installed, either get it from http://www.ruby-lang.org , or use the package management system of your distribution.
      • Install RubyGems:download it from http://rubygems.rubyforge.org, unpack and install with
        sudo ruby setup.rb
      • Install rails:
        sudo gem install rails --include-dependencies
  • The tutorials
    • Probably the simplest thing after you have installed Rails is to start with Four Days On Rails. It is a simple and practical guide to get a quick overview of the possibilities of Rails, illustrated through building a real-life application.
    • Another very good inroduction is Curtis Hibb’s Rolling with Ruby on Rails. It is more detailed than the first tutorial, also includes a full installation guide (for Win32). Part 2 can be found here, and Ajax on Rails is the third part dealing with (surprise!) Ajax in Rails.
    • Amy Hoy has another nice introduction here.
  • Books
    • The bible of Rails development is definitely the Jolt-award winner Agile Development with Rails. If you would like to do more serious Rails development than just write some simple applications after reading the tutorials, i definitely recommend you to buy this book. The tutorials are very good to get the idea of what is RoR about, but they can not substitute the book.
    • There are more interesting books out there, like Rails Recipes or Ruby for Rails but to get you started, the above mentioned book should be enough. If you want more, be sure to check out these as well.

Ruby on Rails and Ubuntu

Although i have nice Ruby on Rails support at dreamhost, i have decided to install RoR to my local machine. IMHO a local RoR, combined with Radrails and WEBrick is the simplest and quickest solution, usually fully enough for development purposes.

To install Ruby on Rails, you just need to do 3 things:

  1. Install Ruby
  2. Install RubyGems, a package management system for Ruby
  3. Once you have RubyGems installed, it is a piece of cake to install new packages. In this concrete case:

gem install rails –include-dependencies

And you are on rails! This was easy, wasn’t it?

Well, yes, unless you happen to use Ubuntu .

Under Ubuntu, the MySQL-Ruby binding was outdated. No problem, just get the newest mysql-ruby gem:

gem install libmysql-ruby
At this point the trouble begun. Instead of installing the mysql binding, i got every kind of nasty error messages, including:

ERROR:  While executing gem ... (RuntimeError)
ERROR: Failed to build gem native extension.
Gem files will remain installed in
/usr/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
ruby extconf.rb install mysqlnchecking for mysql_query() in
-lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no

WTH?! I have googled a day to find the problem, then also wrote to the RoR and Ruby mailing lists, but got no answer. As i came to know later, this happened because my problem had nothing to do with Ruby, Rails or MySQL.
After several hours of trying every possibility, i have been checking the last one: Do i have gcc and make on my machine? Well, i did not! I have thought a decent linux distribution should have make, gcc et al – and i was wrong (considering that Ubuntu is a decent distro).

Ubuntu is a very nice distribution, for desktop usage. However, if you are planning to use it for development, compiling etc., double check whether you have basic development tools (apt-get install build-essential), gcc and whatever you may need for compiling/deploying your applications.

Or install Rubuntu