Ruby on Rails: First Use
So, recently I just started a project with Ruby on Rails. I can’t give details on the project JUST yet, but they’ll be disclosed as soon as I can. For your information, however, you could relate this project to Craigslist. Now, I’ve been designing and developing applications in PHP (sometimes using Smarty, others using CakePHP) for a while now, so I originally blew RoR off as “the easy way out” of actually having to develop applications (for the record, I thought the same for Cake and other MVC systems).
Pre-using of Ruby on Rails: (personal opinion of skills)
- PHP Development: Expert
- CakePHP Development: Beginner/Intermediate
- Python Development: Beginner
- Ruby Development: Absolutely none. All I did was watch the Blog in 15-Minutes Screencast (Quicktime video)
The first applications I ever wrote in PHP used Smarty. I have since written applications that do not use Smarty as a templating language (kind of redundant with PHP anyways, I suppose) and I have also worked with CakePHP somewhat. Not enough to consider myself any better than fooling around, but enough to say that I would be comfortable designing and making a development Cake application.
So, what’s the first thing I can say about Ruby on Rails? It seriously is magic. Continue on to read my compare/contrast of RoR and Cake…
I decided to compare Cake and RoR by attempting to build the same application in each language. Both were built with separate databases, etc. Both are hosted on my personal webspace, hosted by HostGator. I used Ruby version 1.8.7, Ruby on Rails version 2.3.3, and CakePHP version 1.2.4.8284, all were in development mode. For this comparison, I will use the following pieces: Command Line apps (Script/…, Rake and Cake Bake), Model Associations, and basic MVC conventions.
- Command Line Applications
A Rails application comes with a very sophisticated set of scripts that can create files, run your server, and even create a real-time console for your particular application. I used script/generate to do most of my basic model/controller/view creations, which allowed me to have DB tables created on the fly. script/generate is pretty simple to use. It has actions to create models, controllers, views (with controllers), a scaffolded object, database migrations, and can even be augmented by different RubyGems or plugins. I used scaffolding to quickly generate the basic concepts of my pages, which created a model, a controller, views for CRUD, and the related migration file. The aforementioned screencast made it very easy to understand how all of this worked. It may not have helped me really understand the code (scouring of RailsGuides, Stack Overflow, and Google searches, however, did help) but I felt at home when trying to use the script/generate command.
script/generate, however, is not the only option I used. script/server is very simple. With this running, I was able to hit port 3000 on my machine and view the application as it stood in real time. If something didn’t work exactly the way I wanted it to, I could attempt to fix it and it updated in real-time (apparently Rails in production doesn’t work the same way? Or perhaps I read something wrong somewhere.) Also, script/console allowed me to play around with various pieces of code so I could see if what I thought should work, actually worked. All in all, I was very pleased with the options given to me.
Rake is kind of like the UNIX make command. A Rails application creates a Rakefile in the main app directory, and can be called from any child folder in the structure without having to directly reference it. It has no hard coded functions, but pulls the tasks from a separate list of .rake files. I’m not entirely sure how it all works (I have yet to delve there) but it is pretty fancy. My only usage of rake involved the rake db:migrate and rake routes functions, so rake is probably much more functional than I know.
Cake, on the other hand, only has one option for command line applications. Keeping with the Cake theme, they named the app cake bake. In Cake, you “bake” an application. Rather corny, but it has me trying to figure out why RoR has rake. Anyway, cake bake is very different from the script/... functions in RoR. cake bake seems to be almost like a standalone application in and of itself. Thankfully, cake bake has most of the functionality of the script/... functions. Cake’s method of creating models, controllers, views, etc, works slightly differently than script/.... With Cake, you create all of the database tables, etc. yourself. When you create models, Cake will auto attempt to interpret any associations you may have with your project. So, if you put a user_id column inside of a table, Cake will ask you if that object “belongs to User.” If you are building User, and some other table has a user_id column in it, Cake will ask you if “User has many” or “User has one” of the object you’re creating.
Well, that’s it for this round. I’ll post the link for part two when I finish writing it.