Powered by Blogotomy Identity Password

Frequency

Rails rocks.

Tue Jul 5th 21:33:39 2005

I've been developing my personal website using Ruby on Rails, and it's coming along quite nicely. The only problem I'm having is that I can't get the hang of creating new objects. I can't get it to work. The display part was really simple, though.

One of the coolest parts -- the part that makes it easiest, actually -- is that you simply use database objects just as if they were regular Ruby objects. Let's say you were implementing a blog (been thinking about putting one on there, just to compare Rails to Zope), and wanted to get all the entries for a user. Check this out:

class Entry < ActiveRecord::Base
belongs_to :category
belongs_to :user
has_many :comments
end


That defines the Entry model and tells the system how it relates to the Category, User and Comment models. This way, if you want to get the name of the user that posted a certain entry, rather than getting the user object associated with the numeric user id, the call to entry.user gets all the information about the user. Same with the category. If you want to get the name of the category this entry belongs to, you call entry.category.title.

Then, to display the comments, calling entry.comments returns an array of Comment objects, which you can iterate through to like so (in the template -- called the "view" in keeping with the Model-View-Controller paradigm):

<% @entry.comments.each do |comment| %>
<%= comment.user.name %>:<br/>
<hr><i><%= comment.title %></i><br/>
<%= comment.body %>
<% end %>

It wasn't even this easy in Zope. You had to write SQL methods for everything the database did. I have yet to write a single line of SQL code for this. I am very impressed.

dudeman

Wed Jul 6th 21:55:31 2005

Will GrokThis setup rails for you? BTW, I have been using Rails for a little while and really enjoy it.

Add a comment
© Copyright Blogotomy.com 2003-2008
Memberlist
9 viewers online
()  9 Guests
Natural Soap