Brian Corrales
corralesonline.com

Rails Conference 2008

June 2nd, 2008 . by brian.corrales

This past weekend I was in Portland, Oregon attending Rails Conference 2008.  It was a great event and I learned a lot.  The event lasted from Thursday to Sunday and I was able to attend quite a few tutorials, sessions, and presentations.  There were nearly 2000 rails developers in attendance!   I was impressed with the Conference Center and the catered meals that were provided.  I thought the event was very organized.

What really stood out for me was the presentation on Rails 2.1 which was made available over the weekend.  Rails 2.1 has a lot of great new features including better caching, gem dependencies, and time zone support.  One new addition that really struck my attention was versioned migrations.  A time stamp will now be at the beginning of the migration filename.  Having version control to my migrations will save me a lot of headaches.

Now you can do this:

script/generate migration one
      create  db/migrate/20080402122512_one.rb

The number at the end is a time stamp.  You can then retrack your steps by typing:

rake db:migrate:up VERSION=20080402122512

One other cool addition is the modification to ActiveRecord.  Say you wanted to find all your blog posts.  The command would be Post.find(:all).  With Rails 2.1, we’ll be able to simply write Post.all and Post.first.  Seems pretty intuitive.

I know it’ll probably take a little bit of work to upgrade my applications, but you gotta hand it to the Rails Team.  Version 2.1 is awesome!


			

RAILS 2.0 Sessions

February 13th, 2008 . by brian.corrales

So I just updated a project to RAILS 2.0. There’s been a few stumbles on the way, but the main issue I came across was Sessions. It seems that the RAILS team has decided to move away from file-system sessions to cookie sessions. Cookies seem to be much faster than the old file system. The only draw back is that it forces you to utilize session variables as intended…sparingly. Cookies are generally limited to only 4K. There really isn’t any need for you to ever store more than this in your session anyway. It’s just not considered best-practices.

So if you are upgrading to Rails 2.0 and you get some errors, such as a 500 Error, be sure to check how you are handling sessions. Regardless, you’ll need to add the following code into your environments.rb file (don’t forget to restart your server):

config.action_controller.session = { 
  :session_key => _my_app_session, 
  :secret      => hashed_key_of_at_least_30_characters 
}

If you want to read the documentation, here’s the link: http://caboo.se/doc/classes/CGI/Session/CookieStore.html .

For a really good discussion on Sessions and RAILS, go here.


Ordering Eager-loaded Data

February 1st, 2008 . by brian.corrales

If you are using Active Record with Ruby on Rails, I’m sure you’re familiar with the :include symbol for the find method. Here’s an example where we have a category structure of Categories -> SubCategories:

@category = Category.find(params[:id], :include => [:sub_categories])

This will find the category with id = params[:id] and will eager load all of the related sub categories. This way we only need to run one query, rather than two. We could also do this

@category = Category.find(params[:id], :include => [:sub_categories, {:sub_categories => :products }])

Now we are going down another level and retrieving all products related to each sub_category. Unfortunately, there is no way to go travel further into your data model with the :includes symbol. If anyone has a good approach, I’m all ears.

The problem I’ve run into is that my Category model has has_many :sub_categories, :order => ‘rank asc’. When I include :sub_categories, Active Record does not recognize my ordering. This caused me quite a few headaches until I came across this in the docs:

” Since the eager loading pulls from multiple tables, you‘ll have to disambiguate any column references in both conditions and orders. So :order => “posts.id DESC” will work while :order => “id DESC” will not. Because eager loading generates the SELECT statement too, the :select option is ignored.” Here’s the link:

So the best approach would be @category = Category.find(params[:id], :include => [:sub_categories, {:sub_categories => :products}], :order => “categories.rank, sub_categories.rank”)

This will order your categories and sub categories. Hopefully someone finds this useful. If you have come up with a better approach, please let me know.


RMagick Errors with Windows

December 24th, 2007 . by brian.corrales

If you are experiencing an unusual ruby error after installing RMagick, before digging too deep, try not only rebooting your web server, but Windows itself.  That fixes a ton of possible errors that could result from a RMagick installation. The error I had was RMagick.so not found.


Ruby on Rails Error

July 4th, 2007 . by brian.corrales

I’ve been working on this for a long time.  I get this error when trying to work a sql statement like so:

@names = CommonGivenName.find(:all, :conditions => [ “common_given_names.name LIKE                                   #{params[:descendant][:given_name]}%’”])

I keep getting this error:  malformed format string.  I couldn’t figure out the problem, but apparently, Rails doesn’t like the % sign.  After adding a second one, the query ran just fine.  If anyone has any ideas on this, I’d be interested in learning.  I just know it works this way.


Ruby on Rails vs PHP

June 3rd, 2007 . by brian.corrales

Alright, so my next post is technology based. I’ll get to some martial arts stuff soon I promise. Jimmy showed me this youtube video on these guys did a superb job at explaining the joys of Ruby on Rails development. Imagine setting up a database schema and throughout development you add a table here, or a column there. Your code and db schema evolve throughout the process. Then BAM, you need to revert your application code to a previous version. I’ve found it quite difficult to revert my db schema back to the same version. Well, Ruby on Rails has a nice module called migrations. Migrations are 100% Ruby code which can create your db schema for you. As you add tables, modify a column, or whatever you need to during the db schema lifecycle, migrations keep a record of every change you make and an easy way to revert at any time. Once migrations are set up, reverting is as easy as typing “rake db: migrate VERSION=10″.

Enjoy the film:


Mr Rails discusses the RAILS Framework

January 6th, 2007 . by brian.corrales

This is a great link for anyone interested in understanding more about the future of Ruby on Rails.  Hear it from the creator himself!  For me, this interview was really really informative.  Anyone who is into agile, iterative development, would really benefit from RAILS.  Here’s the link:  http://www.infoq.com/interviews/David-Hansson


Ruby

December 27th, 2006 . by brian.corrales

So I’ve been programming in CAKE PHP for about 6 months now and have really enjoyed the MVC framework. I’ve heard that Ruby on Rails is even slicker so I’m transitioning to Rails now. Last night I spend some time learning about Ruby. A friend of mine showed me Try Ruby! (in your browser) where you can learn to program Ruby directly from the browser! It was actually pretty fun and it starts you off with the very basic elements of programming. If any of you are not developers yet, but are interested in learning, this new Try Ruby! website is for you!

So far, I’m enjoying the syntax of Ruby. Most computer languages takes some getting used to. As I’ve learned Java and PHP, I”ve had to change my way of thinking to accomodate the language structure. Ruby, by contrast, accomodates the human programmer and the syntax of Ruby changes. For example, let’s say I wanted to print “Hello World!” five times in a browser, with a break after each line. In PHP, it would look like this:

for($i=0; $i<5; $i++) {
echo "Hello World!";
}

This says start with "i" equalling zero and print "Hello World to the console everytime. Add one to "i" everytime and continue the print until "i" is no longer less than 5.

Ruby, on the other hand, has much more intuitive syntax:

5.times { print Hello World! }

This says exactly what is on the screen: Print “Hello World!” five times.
Not only is this an easier syntax to read and manage, it’s shorter and more concise. If anyone has some excellent online tutorials on Ruby and/or Rails, I’d be very grateful. So far, I’ve been reading Why’s Guide to Ruby. So far I’ve found it very helpful and an easy read.