Three reasons to love Rails Thursday evening, 6 April 2006
I’ve finally got back into some proper rails development and finding out about this framework. I’ve been really enjoying using SVN and getting my hands dirty with all the command line stuff as well, but today I want to talk about three things that have made me love rails whilst updating an old site to have a simple CMS. In no particular order:
Reason One
find_by_columnName(params[:id])
I have no idea how this works but it is amazingly cool. Rails has a whole ORM (Object Relation Mapping) thing going on so that you probably won’t have to write any SQL. You can append any column name for a table to ‘find_by_’, pass it anything you want, and rails will write the sql to search on that column for you. It’s like magically written methods that automatically work for your situation.
Reason Two
validates_uniqueness_of :name, :scope => "page_id"
In ActiveRecord (the bit that handles the mapping of your database to objects) you can apply various filters and validations to your objects. This one checks that a column is unique, like a primary key. But just to make it even cooler you can make it unique in the scope of another column. This example is for a snippets table that stores snippets of text for different pages (which are stored in the database). In the view you can plop in the snippets by using their name, which is nicer than some number. In order to work no two snippets can have the same name, but only on a given page. So there are two snippets with the name ‘introtext’ but they are unique to the page in the database (the column page_id).
Reason Three
before_save :htmlize def htmlize if(self.edithtml == 0) self.html = RedCloth.new(self.plaintext).to_html end end
Again in ActiveRecord, filters let you do things at various points in the life of an instance of a model. The filter ‘before_save’ lets you call a function to process data before the model is saved back to the database as a record. This one takes the field ‘plaintext’ which can be augmented with textile, does a conversion to html with the RedCloth library and puts it into another field ‘html’ before it gets saved in the database.
Comments
Get Around
Journal
- contemplating.Thoughts from a Christian world-view.
- enjoying.Reviews of stuff i've been enjoying.
- life.For those that would like to know what i'm up to, this is the place to look.
- working.Thoughts and ideas on web development and projects i'm working on.
Other Places
- Flickr. Home of my photos.
- Artykins. My fiancé’s blog.
- HydeStreetChapel.org. My church.