How to trim a string and fill remaining space in Ruby

So I needed a way to take a string, and depending on its size, shorten it and fill the remaining space with some arbitrary sequence of characters. Ljust would work, except for the fact that it will fill a string up to a given length, but I only needed to do this when over a certain size. Here is an example:

str = "Somereallylongstring"
if str.length > 10
  puts str[0..7] + "…"
  puts str
# => "Somerea..."

This is the basic idea of what I wanted to do. I decide to make it cleaner and override the String class like so:

class String
  def lfill(len = self.length, fill = ".")
    tmp = self[0..(len – 3)] + "..." if self.length – 3 > len
    return tmp || self

nstr = "Somereallylongstring"
puts str.lfill(10)
#=> "Somerea..."


acts_as_similar – A basic similarity activerecord plugin

I had a need to find items that were similar to an item. I looked at acts_as_recommendable, and while this was very nice plugin, it was too slow when working with large data sets. I also decided that the results I needed didn’t need to be scientifically accurate, just a rough match. So I created this plugin to allow for a very elementary way to find items that are similar to the object you are working with. This works best when used with a has_many :through relationship.
Basically all it is doing is looking for other objects of the same class, that have some related value.

Git it here:

Example 1

Look for similar playlists, using the videos as what defines similarity.
This will look for all playlists that have a similar video as the playlist you are looking against.

class Playlist
  has_many :playlists_videos
  has_many :videos, :through => :playlists_videos
  acts_as_similar :videos

Example 2

Look for similar playlists, using the title of the playlist as the similarity item.
This will look for all playlists that have a similar title as the playlist you are looking against.

class Playlist
  has_many :playlists_videos
  has_many :videos, :through => :playlists_videos
  acts_as_similar :field => :title

Example 3

Look for similar playlists, using the video_id of the playlists_videos as the similarity item.
This will look for all playlists that have a similar video_id as the playlist you are looking against.

class Playlist
  has_many :playlists_videos
  has_many :videos, :through => :playlists_videos
  acts_as_similar :playlists_videos, :field => :video_id


@playlist = Playlist.first

Note: This is a work in progress.

ActiveRecord – Making :include and :select play nice!

So I have been using a nice plug-in that will allow me to using :select when using :include, and not have it pull the entire data set. You can add the plug-in to your app like this:

script/plugin install git://

Here are some ways to use the plug-in:

Employee.find :all,
  :select => ", address.state, employees.*",
  :include => :address

Employee.find :first,
  :select => "now() as current_time,, DATE(addresses.created_at) as addresses.created_at, employee.*",
  :include => :address

Employee.find :all,
  :select => ",, employees.start_date",
  :include => :address

Examples taken from:

Some Possibly less know ActiveRecord configuration options

I am going to list from the site, some possibly less know, but useful configuration options for ruby on rails. I didn’t know these existed until I had to figure out an elegant way to prefix tables with something, and I didn’t want to just type it into the migrations and models. Here is how you set the variables. In your config/environments/development.rb (or production.rb, or whatever.rb) do the following:

config.active_record.table_name_prefix = "whatever_"

You can also select any of the following options instead of table_name_prefix to configure active record globally. Here is the list:

ActiveRecord::Base includes a variety of configuration options:

  • logger accepts a logger conforming to the interface of Log4r or the default Ruby 1.8.x Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling logger on either an ActiveRecord model class or an ActiveRecord model instance. Set to nil to disable logging.
  • primary_key_prefix_type lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named id (and this configuration option doesn’t need to be set.) There are two other choices:
    • :table_name would make the primary key for the Customer class customer_id
    • :table_name_with_underscore would make the primary key for the Customer class customer_id
  • table_name_prefix lets you set a global string to be prepended to table names. If you set this to northwest_, then the Customer class will look for northwest_customers as its table. The default is an empty string.
  • table_name_suffix lets you set a global string to be appended to table names. If you set this to _northwest, then the Customer class will look for customers_northwest as its table. The default is an empty string.
  • pluralize_table_names specifies whether Rails will look for singular or plural table names in the database. If set to true (the default), then the Customer class will use the customers table. If set to false, then the Customers class will use the customer table.
  • colorize_logging (true by default) specifies whether or not to use ANSI color codes when logging information from ActiveRecord.
  • default_timezone determines whether to use Time.local (if set to :local) or Time.utc (if set to :utc) when pulling dates and times from the database. The default is :local.
  • schema_format controls the format for dumping the database schema to a file. The options are :ruby (the default) for a database-independent version that depends on migrations, or :sql for a set of (potentially database-dependent) SQL statements.
  • timestamped_migrations controls whether migrations are numbered with serial integers or with timestamps. The default is true, to use timestamps, which are preferred if there are multiple developers working on the same application.
  • lock_optimistically controls whether ActiveRecord will use optimistic locking. By default this is true.

How to merge strings just like ActiveRecord conditions do

While you can achieve the same functionality using sprintf, this may provide a cleaner approach and one that you are more familiar with. This will allow you to build a string the same way you can use ActiveRecord and the :conditions option.

Basically how this works is by overriding the Array class and adding a method to merge the string and values together into unified string! Enough talk, lets see some code:

class Array
  def merge
    statement, *values = self
    expected = statement.count("?")
    provided = values.size
    unless expected.eql?(provided)
      raise "wrong number of bind variables (#{provided} for #{expected}) in: #{statement}"
    bound = values.dup
    statement.gsub("?") { bound.shift }

As you can see, if you do not provide the right number of values for the statement, it will raise and error. Here is how you would use it:

puts ["Hello ?, how are you", "John"].merge
#=> Hello John, how are you

Likewise, you can use variables to hold values:

message = "Hello ?, how are you"
name = "John"
puts [message, name].merge
#=> Hello John, how are you

This will also work with multiple values:

puts ["Hello ?, ? and ?, how are you", "John", "Joe", "Jim"].merge
#=> Hello John, Joe, and Jim, how are you

The only downside to this currently is that you cannot use a ? in the string you are merging, as it will think its a binding character.

NoMethodError on nil.each?

Have you ever have to iterate over an array from say the params hash? You probably have added some code to make sure that data exists in the hash element first before doing the loop. This is a cool little trick to help condense some code. Normally I would write this:

if params[:accounts] and params[:accounts].size > 0
  params[:accounts].each do |account|
    # Do something with account here

As you can see, kind of ugly. Here is the change:

(params[:accounts] || []).each do |account|
  # Do something with account

AHH!!! Much nicer!

Moving files in Rails using String

Since ruby allow for really easy overriding of objects and classes, I decided to make it easier and cleaner to move my files. Here is how you currently do it:

FileUtils.move("/path/to/file", "/path/to/new/file")

I know this isn’t a huge deal, but I would like to clean it up a little bit. I used an initializer (RAILS_ROOT/config/initializers/core_extensions.rb) to override the String class as such:

class String
  def move(to = nil)
    if to && File.exist?(self)
      FileUtils.move(self, to)

This allows me to do the following to move files:


I though it was pretty cool!

Turn off Rails sessions by request type

Here is a solution to turn sessions off based on a request type. I added this to application.rb file:

session :off,
  :if => { |req| ['xml', 'rss'].include?(req.params[:format]) }

You could ideally just add this to an individual controller, and add :only or :except to it as well, but I wanted to eliminate all of the controllers and actions.

Easy True/False Select Tag with Rails

It’s been a while since my last post. I have been pretty busy with other things, but thats no excuse. Anyway, I came across a problem where I needed to create a select tag with just a true and false option in it and came up with the following quick and easy solution. (This would presumably work with more/different options as well): First, create a file in the config/initializers directory. This is where your global variables will go for use in the select tags. I called mine select_tag_globals.rb:

TRUE_FALSE = { 'True'=>true, 'False'=>false }

Make sure the values are set to whatever the fields data type is. In this case I want the labels to say True or False, and the value saved to the database will be true or false. Now all we have to do is use this in a select tag in a view like so:

<%= :database_field, TRUE_FALSE %>

This should give you a normal select box with True and False labels. Yay rails!

ActiveRecord and Transactions!

If you ever have to do multiple actions with activerecord, you should group it into a transaction like so:

Post.transaction do
  posts.each do |post|
    post =>'Whatever')!

This will rollback too if the save fails.