I'm working on a project where the user can reset their password if they've forgotten it. So, I need to generate a random password and email it to them so they can login and change it. Fortunately, I found this neat little Ruby snippet that will generate random text of a given length:

When using Ruby on Rails, ERb (embedded Ruby) is used a LOT in the X/HTML. There are two types of ERb,

An evaluation block:

<% some_code %>

And an output block:

<%= @print_this_variable %>

These blocks are necessary when using Rails, and I've noticed that when I do a 'View Source' (via the Web Developer addon for Firefox, of course), I see a lot of funky spacing and line breaking where the ERb's have been evaluated. Probably fine for most people, but it makes reading the outputted HTML code a hassle.

It turns out that there are really three ways to use the evaluation ERb that can affect your spacing and line-breaking.

Firstly, the output block is used just like you'd expect to use it. If your code says this:

<p>
Text before ERb.
<%= "code_goes_here" %>
Text after ERb
</p>

The resulting HTML will look just like this:

<p>
Text before ERb.
code_goes_here
Text after ERb
</p>

Evaluation block use #1: If you just put a block, it will cause a line break after the block in the HTML, so if you had this in your code:

<p>
Text before ERb.
<% some_code %>
Text after ERb
</p>

The resulting HTML code would look like this:

<p>
Text before ERb.
Text after ERb
</p>

Evaluation block use #2: If you add a dash(-) at the end of the block, it will prevent it from adding a line break. So, if your code looks like this:

<p>
Text before ERb.
<% some_code -%>
Text after ERb
</p>

The resulting HTML code would look like this:

<p>
Text before ERb.
Text after ERb
</p>

The line break is gone, but the space taken by the block is still there. That leads us to. . .

Evaluation block use #3: If you add a dash(-) at the beginning AND end of the block, it will prevent it from adding a line break AND remove the leading space it would have taken up. So, if your code looks like this:

<p>
Text before ERb.
<%- some_code -%>
Text after ERb
</p>

The resulting HTML code would look like this:

<p>
Text before ERb.
Text after ERb
</p>

You'd never know there was a code block there! Is this useful? I don't know... maybe.

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:

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:

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: