Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Wednesday, May 4, 2011

Speed up your counts in Rails 3

Just bumped into this and thought I would share. If you are trying to find the number of records that match two columns in your schema you might do this:

find_all_by_some_column_and_some_other_column('val', 'val2').count

What this does is select all of the data that matches and then does a count in Ruby. This is slower because it retrieves all of the data for every matching record. Whereas if you count like this:

where(:some_column => 'val', :some_other_column => 'val2').count

It will do the count in the database (which databases are really good at) and only return the total number of records that match. If you have 12 records it won't matter much which of the above you use. If you have 12,000 records the second will be incredibly faster.

Saturday, April 16, 2011

Why Ruby and Rails?

"Why should we use Ruby and Rails?" has been popping up at work over the last month. Managers are worried about retraining the Java developers. And the Java developers are worried about wasting their time on some fad. Here are my reasons why you should be using Rails to build a web site.

First, Rails is a web framework, nothing more or less. It claims to do nothing but allow developers to easily create websites. It makes hard problems trivial and impossible problems doable. It claims a 10x improvement in development time. That claim is VERY DIFFICULT to believe, so I will address it later with examples from my current project. Rails should be thought of as a Domain Specific Language (DSL) for web applications. Writing templates or views of web pages is eased with many helpers.

Think of scriptlets in JSPs, but terse code that makes sense. The helpers range from creating links to creating whole forms. All of this goodness is packaged up in an MVC pattern. Unlike Struts, which also claimed to be MVC, there is a single layer for Controllers; each layer is contained in its own directory. Another benefit is the directory layout is standard. If you know the directory layout, every Rails application will be familiar to you. This is a big benefit after you have switched to your upteenth Java project which has it's own directory structure, completely different from all of those that came before it.

Second, you get RESTful web services for free. I can't stress this benefit enough. When you use the generators for Rails you get the server side of a the web service. Building the client is trivial. Remember I said I would prove the 10x improvement in developer time? We are building an application which will have many clients. The clients are mostly Java, so a developer was assigned to write the client for the RESTful Rails application. He decided to write a parser in Java. This took a couple of weeks to write and test. And from time to time we find new bugs in the code.

However, we could have written three lines of Ruby that would have done the same thing. I literally implemented the same thing he had been working on for two weeks in 10 minutes. 8 of those 10 minutes were trying to find the documentation on what needed to be written (sadly when I showed him the Rails way he shrugged and said, I know how to do it this way and I already have this time spent on it. Maybe I should have explained sunk costs). 10 minutes vs 2 weeks, 10x improvement in speed might be an understatement. This isn't the only way Rails improves implementation speed. Writing database queries are a breeze.

Rails comes with its own Object Relational Mapping (ORM) tool called ActiveRecord. Similar to Java's Hibernate in functionality, ActiveRecord improves development by making complex is easy. Rails provides generators to create scripts to create tables in your database of choice (support for many databases is provided including MySQL and Oracle). The generator also creates a Model class for each table you have in the database. The Model class is where you write the code for business logic and any methods needed to access the data. Except, you don't need to write much because Rails provides many access methods without the developer needing to write any code. Rails also helps by providing helper methods to create relationships between objects. ActiveRecord provides support for one to one, one to many and many to many relationships with just a single line of code in each Model class or two lines of code in each Model class for many to many relationships (technically you can do many to many with one line of code in each Model class in the relationship, but that method has been deprecated in favor of the more flexible two method approach).

The last time saving feature of Rails is that you can use Ruby gems in your web applications. Gems are like Java's jar files, they are packages of code that can be thought of as a library. Gems easily support dependencies so having a Gem that requires ActiveRecord is easy to create, which means you can create Gems that natively work in Rails applications. Need a User object in your application? Download one of the many authentication gems available on the web. Find the one that suites your need, install it and you are ready to go. Need to upload files? Install a file upload gem. This capability is available in Java applications too, but for some reason they are more complex.

Finally, Rails is a simple and easy way to get a web application up and running. A developer can finish a week long Rails course with enough fundamentals to be ready to go write a Rails application.

Friday, May 8, 2009

Getting Started with Rails

A friend just asked me how to get started on Rails. I thought I would share my thoughts with the world also:

First, buy the books from the PragProg guys:

Don't worry about getting the Programming Ruby 1.9, it has another 6months to a year before it will be the preferred way to go. The community hasn't shifted to it yet. Just be aware that it is coming. Both of these books are must haves and really the only requirement to get started.

Second, I highly recommend buying a Mac or installing Linux. It can be done on Windows, but it just isn't fun. There will be a ton of new things that you are going to have to learn and being on a Mac usually gets you answers faster because so many people are using them. And the majority of the screencasts are on Macs. This isn't a must, but it will make your life easier.

Third, watch screencasts at PeepCode
You can skip the PeepCode screencasts if you don't want, but I feel like they are the best way to get started. Be sure to buy the pack of 5, it will save you $10.

Git is a distributed SCM and is really important. Everything in the community is moving to it. Rails is hosted on GitHub as are most of the libraries that you will need. If what you are doing is open source, then using GitHub will be free. They have excellent documentation to learn about Git on their site.

Fourth, watch more Rails Screencasts. These are free. Basically, you will want to search to see if he has answered a problem you have.

Fifth, bookmark the RailsApi. This is the best Rails API site I have found.

Deployment is going to be pesky, but I highly recommend Heroku. To deploy, you just push your git repository to the server and they take care of the dirty work for you. It is awesome. And brand spankin new... So there are times when it has issues but they are usually resolved rapidly.

Subscribe to the Rails talk mailing list. A good resource when you have questions.

I hope this helps a new Rails developer get started!