Posts Tagged with "rails"
Rails eager loading of associations
FEB
18
2007
I ran into an interesting problem with table associations at work the other day. Below is a simple data model of the tables I was working with. Basically, here's how it breaks down: A user is part of any given group (but only one group per user). A document (any type) can be uploaded and assigned to a folder. A group is used to assign viewing privileges to each document, so that every user in the group can see the document. See below:
The problem came up because once a user is logged in, I need to get all of the folders that contain documents that the user's group has permission to see (a folder could contain documents not viewable by the group). I spent a few minutes scratching my head on how best to do a find to get the results I needed. I really couldn't come up with anything that would work.
So, of course, when this happens, I do a little leg-stretching and walk over to Adam and Shawn's space to ask them how they would do it. After explaining the situation and a little white board art, they reminded me of eager loading of associations (which I've never really had to use).
By using the :include option in my find method call, I can pre-load table associations to make my complex query a lot easier. My code ended up looking like this:
# find the logged-in user
user = User.find(session[:intranet_user])
# get all of the folders that contain documents they have access to
@folders = Folder.find(:all, :include => {:documents => {:groups => :users}}, :conditions => "users.id = #{user.id}" )
This worked perfectly!
A note about eager loading of associations: they can save a lot of resources when used correctly. Say you have a table called books and a table called authors, and each book has one author. To get all of the books for display, you might put:
@books = Book.find(:all)
Then, in your page, you might have:
<% for book in @books -%> Title: <%= book.title %> Written by: <%= book.author.name %> <% end -%>
Not only do you run a SQL query to get all of the books, you then run an additional query for each book to find it's author. You can simplify this down to only one query, by using eager loading, like so:
@books = Book.find(:all, :include => :author)
This pre-loads all of the data with only one SQL query.
Tagged: eager loading, rails, activerecord, db, tutorial
RailsConf 2007
FEB
02
2007
They opened up registration today for RailsConf 2007. It's happening May 17-20 in Portland, Oregon. Adam, Shawn and I are registered and raring to go. Looking at the sessions planned for this year's conference, it looks to be very interesting and informative. Can't wait.
OS X Leopard to have Ruby on Rails
AUG
09
2006
I received a jubilant email today from Shawn that contained a link to this article officially announcing that Mac OS X Leopard will indeed be shipped with Ruby on Rails pre-configured.
For anyone who's followed these great directions to get RoR up and running on a Mac knows that it takes a fair amount of time and a good bit of focus to get everything correct. Having everything pre-installed will save many people a great deal of aggravation.
On top of having Ruby and Rails in the box, they've promised other packaged goodies like Mongrel, a Ruby alternative to Apache that I've heard is much more efficient.
It's not like I needed another reason to look forward to the OS X update, but now I'm truly excited to get my hands on Leopard.
Why Ruby on Rails is better than .NET
JUL
10
2006
For the past four days at work I've been assigned to apply a redesign to an older .NET site that we did a couple of years ago. It has been infuriatingly frustrating trying to apply a CSS-based design to a table-based .NET site. The way .NET handles almost everything is so convoluted and esoteric that it took me a full day just to get the hang of how it handled displaying markup.
And what is the deal with the datagrid?!?! It's great on paper, but crap in practice. Just the datagrid definition spanned five lines. Using the AlternatingItemStyle tag to get alternating styles for the table rows is ok, but Rails makes it SOOOO much easier with the cycle method. And speaking of styles, the datagrid has to have each style that you'd like applied declared seperately which makes for the huge datagrid definition. Also, take a look at the HTML that a datagrid produces. You'll be surprised at how much markup it can create for a simple <td>.
Now, don't get me wrong. I can see where .NET would be useful and I'm sure there are a ton of examples where it would make much more sense to use .NET than Rails. I think my biggest problem is that I know almost nothing about .NET. I can get by with making visual changes, but I stay far away from the codebehinds.
Closing thoughts: give Ruby on Rails a try! You might just like it!
Tagged: rails
Syndicated!
JUN
16
2006
One of the things I had hoped to be able to do with Rails for this blog was to create an RSS feed. I briefly looked at the chapter that describes how to do this in the Rails Recipes book and kinda decided put it off until later.
Well, after seeing that Adam added an RSS feed to his blog and even provided the steps he took to accomplish it, I decided to finally implement it. It turned out to be a lot easier than I thought.
So, if you're really bored often, go ahead and subscribe.
Popular Posts
Search
Tags
actionmailer activerecord ajax apache apple barcamp caching capistrano centos code golf css db delete eager loading ebay email attachment erb flash ftp fun generators get haml helpers ie sucks javascript jquery lightbox lost merb net ftp paperclip passenger php plexus post presentation rails rails machine railsconf redesign rest rjs routes rss ruby ruby on rails safari script sinatra symfony text replacement tips tutorial twitter xhtml
Projects
© 2010 Travis Roberts. All rights reserved.