Rails
June 18th, 2008
So it’s been a while since I last updated, though it’s hasn’t been a few months like it’s been before. Anyway I started playing around with Rails again this past weekend and it is the most amazing programming language I have ever used. I’m building out the new thefridaycontest.com in it and in 10 lines of code and only 15 minutes I had voting taken care of, including setting a limit on requests by IP address. I’m working on a new site for my cousins business in it as well.
What I like most about Rails is the database interaction. The steps to setup a database table for content [on my MacBook] are:
- Generate a model with Terminal.app (script/generate model content title:string body:text)
- Migrate the database in Terminal. (rake db:migrate)
Fill the table with some content and then from the controller in your page definition (index, new, edit, show, etc.) you simply write:
@content = Content.find_by_id(params[:id])
That basically tells the controller to use the Content model and then find the record by it’s id (which in most cases is the third part of the url after the .com/, so in content/show/1, 1 is the id). After that in the view it’s just simply a matter of calling <%= @content.fieldname -%> to write the data to the screen. If I had to do this in PHP it would take at least twice as long since I’d have to create a database in phpMyAdmin, then write a connection script, then query the database, and finally return it as an associative array for easy use in the page.
I’m still learning, but just from experience this has been the easiest language to learn thus far. In the last few months I’ve had to learn a bit of C#/ASP.Net for work which completely sucked and was a royal fucking pain to learn. PHP took me a awhile to learn initially too, but then again PHP and Rails I learned/am learning on my own time, and not because I had/have too.