Archive for the ‘Ruby on Rails’ Category

Quick Tip: Form Partials

Partials are a great way to keep your view code separated logically. Prior to Rails 2.1 if you wanted to reuse a form partial in, for example, a new and edit view, then you needed to pass the form into the partial somehow.
Given the following form partial:
views/users/_form.html.erb

<div>
<%= form.label :name -%>
<%= [...]

Read the rest of this entry »

Quick Tip: named_scope

Have you ever found yourself writing queries like this?

User.find(:all, :conditions => ['state = ? AND created_at > ? AND created_at <= ?', 'active', start_date, end_date], :limit => 5)

I suppose you could refactor this into a custom finder that did the heavy lifting for you…

User.find_all_active_in_date_range(start_date, end_date)

But what if you need that same query where the state [...]

Read the rest of this entry »

Quick Tip: Route Associations

Are you used to writing your routes like this?
map.resources :notes do |notes|
notes.resource :author
notes.resources :comments
notes.resources :attachments
end
Don’t fret, there may be hope for you yet. For these simple routes you can use the has_one or has_many route association options.

has_one – use it for a singleton resource
has_many – use it [...]

Read the rest of this entry »

Quick Tip: has_many :through => checkboxes

It’s really easy to create a many-to-many relationship that can be assigned through checkboxes. Check it out!
Let’s say you have Users and Groups. A User can belong to a Group and a Group can have many Users – we call this a Membership, like so (migrations omitted for brevity):
app/models/user.rb
class User < ActiveRecord::Base
has_many [...]

Read the rest of this entry »

Quick Tip: Capistrano SSH Ports

If you have changed the SSH port number on your server, then you need to let Capistrano know how to connect. Luckily, it’s pretty easy.
Add the following to your deployment file, replacing 8888 with your port number.
config/deploy.rb
ssh_options[:port] = 8888
This will apply that port number to connections made by Capistrano. If you need to specify [...]

Read the rest of this entry »

Rails Class Collisions

While trying to use Integrum’s oh-so-nifty missing spec finder on a recent project, I discovered that it was not working. Hmmm… seems that somewhere along the way Rails added detection for existing constants and won’t allow you to re-generate something. Instead it calls raise_class_collision, which raises a UsageError. Damn them.
Anyway, to work around [...]

Read the rest of this entry »

TwitterBot Gem Released

Tonight, at one of our (in)famous hack-a-mania nights at Integrum, we released a gem for accessing Twitter through XMPP. Read the full Integrum release article and then go get hacking!
I’m excited because not only did I have a hand in the creation of the twitter_bot gem, but I also helped in the development of [...]

Read the rest of this entry »

RailsConf 2008

I’ll be heading to RailsConf 2008 this week in beautiful Portland, OR. I can’t wait to be in one of my favorite cities again. Especially because I’ll not only be enjoying the city, but also learning from and interacting with the Rails community. If you’re going to be in Portland this weekend, [...]

Read the rest of this entry »

GoRuCo 2008 Wrap-up

As I stated in a previous post, I attended the Gotham Ruby Conference this past weekend in NYC. It was a great time with many interesting talks. The weather was amazing as well. I don’t want to spend a lot of time rehashing the conference because you can view the GoRuCo 2008 [...]

Read the rest of this entry »

Caching Locale-specific Dynamic JavaScript Files

I was recently inspired to create some cached, dynamic JavaScript files for a project I am working on after watching Ryan Bates Railscasts episodes 88 and 89.
The basic concept is to create a JavaScript controller that dynamically renders some JavaScript file(s). This allows you to take advantage of ERB in your JavaScript files. [...]

Read the rest of this entry »