Posts Tagged with "ruby"

Rails Tips

Published about 1 month on July 13, 2008  |  0 comments

While there are no shortages of posts listing tips for Rails, or lesser-known Ruby methods, I decided to write this post because these are shortcuts or helpers that I didn't know about until fairly recently. I'm going to try and update this post as I discover new railties and rubyisms along the way, so check back every now and then.


Symbol#to_proc

This is really a Ruby method, but it makes running a collect or map on ActiveRecord objects much easier. Whereas before, you would do this:

@members = Member.find(:all).map { |member| member.name }

Now, you can simplify it to this:

@members = Member.find(:all).map(&:name)

Model Calculations

This will let you do simple calculations on any ActiveRecord model (as long as the field you're calculating is numeric).

Student.average(:grade) # finds the average grade for all students (returns a float)
Student.maximum(:grade) # finds the highest grade
Student.minimum(:grade) # finds the lowest grade
Student.sum(:grade) # the sum of all grades

Custom ActiveRecord Associations

Let's say you have a model called BlogPost and one called Comment. A blog post has many comments, but you also have a boolean field on comments called approved that signifies if it's been decided that the comment is not spam (like most comments are). This is what your BlogPost model might look like:

class BlogPost < ActiveRecord::Base
  has_many :comments
end

You can add custom associations for approved_comments and unapproved_comments to your model to make fetching these comments a lot easier.

class BlogPost < ActiveRecord::Base
  has_many :comments
  has_many :approved_comments, :class_name => 'Comment', :conditions => 'approved=1'
  has_many :unapproved_comments, :class_name => 'Comment', :conditions => 'approved=0'
end

All you have to do is specify the class name of the associated model and the conditions of the find. This makes it possible to easily access these associations from within your views.

You can now show the number of approved comments for each post.

<%= @blog_post.title %> - <%= @blog_post.approved_comments.count %> Comments

You can also easily list out the approved comments.

<%- @blog_post.approved_comments.each do |comment| -%>
  <%= comment.author %> said:
  <%= comment.content %>
<%- end -%>

UPDATE: Shawn reminded me this week about named_scope, which was added in Rails 2.1. It makes custom associations a bit easier and more full-featured. You can get the same approved and non-approved comments with named_scope:

class Comment < ActiveRecord::Base
  belongs_to :blog_post
  named_scope :approved, :conditions => ["approved = ?", true]
  named_scope :unapproved, :conditions => ["approved = ?", false]
end

Notice that now the named_scope declarations go in the Comment model instead of the BlogPost model like the custom associations. You get the approved comments for a post with this line:

<%= blog_post.comments.approved %>

You can also combine named_scope calls when selecting records. Suppose our Comment model now has a named_scope for both approved and unapproved comments, as well as comments made by anonymous authors.

class Comment < ActiveRecord::Base
  belongs_to :post
  named_scope :approved, :conditions => ["approved = ?", true]
  named_scope :unapproved, :conditions => ["approved = ?", false]
  named_scope :anonymous, :conditions => ["name = ?", 'Anonymous']
end

You can use this to find all of the approved comments for a post where the author is 'Anonymous'.

<%= blog_post.comments.approved.anonymous %>

To add even more functionality to named_scope, you can add conditions to the call on the fly.

<%= post.comments.approved.all(:conditions => ["created_at > ?", 2.weeks.ago]) %>

This will pass the additional date condition to the find, making it possible to refine your searches even more.


Render Partial with Collection/Object

This is how I've always done my partials.

The view (index.html.erb):

<%- @posts.each do |post| -%>
  <%= render :partial => 'post', :locals => {:post => post} %>
<%- end -%>

The partial (_post.html.erb):

<%= post.title %> written by <%= post.author %>

I can simplify the call to the partial down to only one line of code (the partial itself will stay the same).

<div id="all_posts">
  <%= render :partial => 'post', :collection => @posts %>
</div>

By specifying the collection option, the view will call the partial post for each item in the @posts array.

But, let's say I wanted to call the partial for only one post. The old way:

<div id="single_post">
  <%= render :partial => 'post', :locals => {:post => @post} %>
</div>

Not terrible, but we can make it easier with the object option.

<div id="single_post">
  <%= render :partial => 'post', :object => @post %>
</div>

This will call the partial, and pass it only the object that we specify. Shorter and cleaner.


RJS with link_to_function

If you have several things on your page that need to be updated, and you want to do it with a javascript call instead of an AJAX call to the server, you can do it by passing a block to the link_to_function method.

Suppose this is my view:

<div id="list">
  <p id="list_title"><%= @list.title %></p>
  <div id="list_items">
    <%= render :partial => 'item', :collection => @list.items %>
  </div>
  <%= link_to_function 'Change it up' do |page|
    page.replace_html 'list_items', 'New Item Title'
    page.hide 'list_items'
  end %>
</div>

This is a poor example, but hopefully you can see what I'm trying to illustrate here.


Simplified FIND in Rails 2.1

With the release of Rails 2.1, they've added some much-needed find methods for easily getting all, first and last records for a model.

@all_posts = Post.all
@first_post = Post.first
@last_post = Post.last

And, of course, you can pass conditions and order options to the new find methods.

@all_posts = Post.all(:order => :updated_at)
@first_post = Post.first(:conditions => ["title LIKE ?", '%Rails%'])
@last_post = Post.last(:order => 'created_at DESC')

Tagged: rails, ruby, tips


Parsing an RSS feed with Ruby

Published about 1 year on June 9, 2007  |  2 comments

Parsing an RSS feed is insanely simple with Ruby. Two lines is all it takes. . .

require 'rss'
rss = RSS::Parser.parse(open('http://www.travisonrails.com/feed/posts').read, false)

Now you'll have an Array of the results, so you can do something like:

rss.items.each { |i| puts "#{i.title} - #{i.date}" }

Tagged: ruby, rss


Generate random text with Ruby

Published about 1 year on June 7, 2007  |  8 comments

UPDATE: Changed the generate_password method slightly based on commenter Dave Burt's suggestion.

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:

def generate_password(length=6)
  chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789'
  password = ''
  length.times { |i| password << chars[rand(chars.length)] }
  password
end

Some examples:

generate_password
>> U48ydn

generate_password(10)
>> QzWXdAkDy5

Tagged: ruby, script


Fore!

Published about 1 year on January 22, 2007  |  0 comments

Shawn recently turned me onto a really cool site: Code Golf. Every week (or so) they post a coding challenge. The challenges themselves aren't overly difficult (well, most aren't). The real challenge is to complete the challenge with as little code as possible. . . literally. Your code submissions are first evaluated to determine if they work like they should, then they count how many bytes your program takes up. The shortest program for each accepted language (Perl, PHP, Python, and Ruby) gets 10,000 points, with each longer program getting points based on their size relative to the winning submission.

I've only been able to complete two challenges so far. Both have fallen around the middle of the group with regards to size. I'm not sure I'll ever know the shortcuts to get my 228 byte program down to 102 bytes! It is a great excuse for exercising your brain, though.

Tagged: ruby


Ruby compressor for JavaScript and CSS files

Published about 1 year on December 11, 2006  |  0 comments

On Friday, Adam and I were talking about the different JavaScript libraries that are available and how bloated they are. They can easily double the page weight of your site. But, if you want to use the pre-canned goodness of Prototype or Scriptaculous, then that's the price you pay. We were trying to find ways to decrease the amount of files a user downloads when they visit our company Web site. One idea was to compress the CSS file and all of the large JavaScript files that we use. Well, I found a cool online service to compress and combine JavaScript files, but there were no readily available sources for shrinking any other kind of file that I could easily find. So, Adam told me to use sed via the command line and use the power of regular expressions to strip line breaks, tabs, and spaces out of the given file. Well, I don't know anything about regular expressions, and Adam couldn't recall the exact syntax off the top of his head. So, he retired to his corner and whipped up a nice little Ruby program to do it for us.

Enter the Shrinker!

You can download the Ruby file below. If you're on a Mac, just copy it to /usr/local/bin then CHMOD 777 it. After that, you can simply run sudo shrinker path/to/file and it will spit out a new file with the extension .shrunk that you can now name anything you want. Windows users can dump the file anywhere they want and then run, from the command line: ruby path/to/shrinker.rb path/to/file to get the same thing.

File: shrinker.rb (UPDATE: This file is no longer available, sorry.)

Tagged: ruby, javascript, script, css



©2008 Travis Roberts. All rights reserved.