Notes posted to Ruby on Rails

RSS feed
October 31, 2008
0 thanks

By images's sub dirctionary to img tag

image_tag(“icons/edit.png”) # =>

<img src="/images/icons/edit.png" alt="edit" />
October 30, 2008
2 thanks

re: Customizing attribute names in error messages

You can use Module.alias_attribute to achieve the same result as overriding human_attribute_name. After aliasing use the new name in the validates_xxxx_of methods or ActiveRecord::Errors.add.

October 30, 2008
0 thanks

Anyone know the order the scopes assemble conditions?

It seems like last scope = first condition in sql. Can anyone confirm?

October 30, 2008
1 thank

can be useful to achieve attr_reader effect


class Account < ActiveRecord::Base 

  def credit(amount)
    self.write_attribute(:balance, self.balance + amount)

  def balance=(value)
    raise "You can't set this attribute. It is private."


this allows fred.account.credit(5) whilst raising an error on fred.account.balance = 1000

October 30, 2008
7 thanks

Can be extended but only with a module

Although not documented, belongs_to does support Association Extensions however it doesn’t accept a block like has_many does. So you can’t do this:

class Account < ActiveRecord::Base
  belongs_to :person do
    def do_something_funky
      # Some exciting code

but you can do this:

module FunkyExtension
  def do_something_funky
    # Some exciting code

class Account < ActiveRecord::Base
  belongs_to :person, :extend => FunkyExtension

And then call it like this:

@account = Account.first
October 30, 2008
2 thanks

Does very litle

This method simply returns the hash itself since HashWithIndifferentAccess by definition support symbolized key access. For a (regular rails) Hash there is ActiveSupport::CoreExtensions::Hash::Keys#symbolize_keys! that “Destructively convert all keys to symbols”.

October 29, 2008
3 thanks

A work-around for adding confirmation to image_submit_tag

Sometimes you may want to add a confirmation to image submit tags but this function does not allow it. To get over this limitation use a normal submit tag and set the src and type properties (set type to “image”)

Code example

submit_tag “Delete”, :confirm => “Are you sure?”, :src => “/images/trash.png”, :type => “image” %>

October 25, 2008
3 thanks
October 25, 2008
2 thanks

Alternative: use 1000.humanize

1.humanize == “1″ 1000000.humanize == “1.000.000″ 1000.12345.humanize == “1.000,12″


October 24, 2008
1 thank

module includes with callbacks

If you write a plugin or module that includes callbacks make sure to define the method and call super after you’re done with your business.

module CoolStuff

def self.included(base)
  # the next line seems to clobber. instead opt for defining an inheritable method
  # base.after_save :chill

module ClassMethods
  # cool class methods

def chill
  self.cool = true

def after_save
  super # if you don't call super, bloggy won't run

end # yes I know this next line is a divisive issue but it’s common enough ActiveRecord::Base.send :include, CoolStuff

class Blog < ActiveRecord::Base

after_save :bloggy

def bloggy


October 24, 2008
12 thanks

Prompt vs. Select

According to the docs in form_options_helper.rb

:include_blank - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.

:prompt - set to true or a prompt string. When the select element doesn’t have a value yet, this prepends an option with a generic prompt – “Please select” – or the given prompt string.

The main difference is that if the select already has a value, then :prompt will not show whereas the :include_blank always will.

October 24, 2008
1 thank

Styling question

How do we style the select boxes and “:” somehow within this method?

Follwup: it seems in Rails 2.1, FormBuilder#time_select didn’t pass html_options to this method. and it’s fixed i

October 24, 2008
9 thanks

Back it up with a unique index

As mentioned briefly above, as well as using this validation in your model you should ensure the underlying database table also has a unique index to avoid a race condition.

For example:

class User < ActiveRecord::Base
  validates_uniqueness_of :login_name

The index can be specified in the migration for the User model using add_index like this:

add_index :users, :login_name, :unique => true

You do a similar thing when using the :scope option:

class Person < ActiveRecord::Base
  validates_uniqueness_of :user_name, :scope => :account_id

Should have a migration like this:

add_index :people, [ :account_id, :user_name ], :unique => true

Note that both the attribute being validated (:user_name) and the attribute(s) used in the :scope (:account_id) must be part of the index.

For a clear and concise explanation of the potential for a race condition see Hongli Lai’s blog.

October 24, 2008
2 thanks

The human side of inflections

Rails 2.2 moves this functionality to the Inflector::Inflections class:

See the ticket and the code that allow the humanisation rules to be centralised in an app.

October 23, 2008
2 thanks

Customizing attribute names in error messages

This can be used to customize attribute names in error messages. See my note in ActionView::Helpers::ActiveRecordHelper#error_messages_for.

October 23, 2008
8 thanks

Customizing attribute names in error messages

By default, the error messages translate the names of the attributes through String#humanize. The way to to change that is to override the ActiveRecord::Base.human_attribute_name method.

For example, if you want to name a column in your database as :www_url and you want to say “Website” instead of “Www url” in the error message, you can put this into your model:

class Person < ActiveRecord::Base
  def self.human_attribute_name(attribute_key_name)
    if attribute_key_name.to_sym == :www_url

Currently this seems to be the cleanest and easiest way. Unfortunately, human_attribute_name is deprecated and may stop working in a future release of Rails.

October 22, 2008
10 thanks

Gotcha when defining :finder_sql or :counter_sql

When setting custom SQL statements in the :finder_sql or :counter_sql queries, if you need to inject attributes from the current object, such as the ID, make sure to disable string interpolation of the statement by using single quotes or %q().


has_many :relationships, :class_name => 'Relationship', :finder_sql => %q(
  SELECT DISTINCT relationships.*
  FROM relationships
  WHERE contact_id = #{id}

Surrounding this SQL with double-quotes or %Q() will expand #{id} too early, resulting in a warning about Object#id being deprecated and general brokenness.

October 22, 2008 - (>= v2.1.0)
2 thanks

HABTM relation

When you want to create a has_and_belong_to_many relation (og just a has_many :through) use this setup.

class CreateCourses < ActiveRecord::Migration
  def self.up
    create_table :seasons do |t|
      t.integer :year
      t.string :period

    create_table :courses do |t|
      t.string :courseCode

    create_table :courses_seasons, :id => false do |t|
      t.references :course, :null => false
      t.references :season, :null => false
    add_index :courses_seasons, [:course_id, :season_id], :unique => true


  def self.down
    drop_table :seasons
    drop_table :courses
    drop_table :courses_seasons
October 22, 2008
2 thanks

Regenerate the JavaScript after each RJS call

I had a sortable_element that was also a drop_receiving_element. After an element was dropped onto this div, an RJS action refreshed the div with the new elements. The div expanded with these new elements, but the sortable portion remained the same size.

To correctly be able to reorder elements after an Element.update call (from an RJS action or wherever), you need to include a second call to Sortable.create in your RJS view (or other JavaScript), using sortable_element_js or whatever method you please.

October 21, 2008
5 thanks

Using a Loading Graphic

If you want to make a little loading graphic, typically you use an animated gif (like a little spinner or something). Both link_to_remote and remote_form_for allow you to easily do this by using the :loaded and :loading triggers to call javascript.

For example:

<% remote_form_for @survey, :loading => "$('loading').show();", :loaded => "$('loading').hide();" do |f| %>
  <%= submit_tag ' Save' %>
  <%= image_tag "indicator_circle.gif", :style => 'display: none;', :id => 'loading' %>
<% end %>

The ‘loading’ parameter used for the ‘$’ prototype selector is the id of the animated gif. It starts out hidden, and is toggled by the loading/loaded triggers.

October 20, 2008
3 thanks


in small cells:

12def number_to_euro(amount)
  number_to_currency(amount,:unit=>'').gsub(' ',nbsp)
October 18, 2008
0 thanks

Single Table Inheritance and Fixtures

All entries for fixtures for classes derived from the base class must go into the fixture file of the base class. Also, their type must be set. Example fixture:

  name: My Company

  name: A Firm
  type: Firm

  name: SuperVIPClient
  type: PriorityClient
  priority: 1
October 16, 2008
0 thanks

Only error message

<%= error_messages_for :order, :header_message => nil, :message => nil %>

Browser view code

<div id=“errorExplanation” class=“errorExplanation”>

    <li>Weight 只有 1000.0</li>
    <li>Volume 只有 10.0</li>
October 15, 2008
29 thanks

List of status codes and their symbols

Note that the :status option accepts not only an HTTP status code (such as 500), but also a symbol representing that code (such as :created), if that makes more sense to you. Here’s a list of which symbols map to which numbers (derived from ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE):

100 = :continue
101 = :switching_protocols
102 = :processing
200 = :ok
201 = :created
202 = :accepted
203 = :non_authoritative_information
204 = :no_content
205 = :reset_content
206 = :partial_content
207 = :multi_status
226 = :im_used
300 = :multiple_choices
301 = :moved_permanently
302 = :found
303 = :see_other
304 = :not_modified
305 = :use_proxy
307 = :temporary_redirect
400 = :bad_request
401 = :unauthorized
402 = :payment_required
403 = :forbidden
404 = :not_found
405 = :method_not_allowed
406 = :not_acceptable
407 = :proxy_authentication_required
408 = :request_timeout
409 = :conflict
410 = :gone
411 = :length_required
412 = :precondition_failed
413 = :request_entity_too_large
414 = :request_uri_too_long
415 = :unsupported_media_type
416 = :requested_range_not_satisfiable
417 = :expectation_failed
422 = :unprocessable_entity
423 = :locked
424 = :failed_dependency
426 = :upgrade_required
500 = :internal_server_error
501 = :not_implemented
502 = :bad_gateway
503 = :service_unavailable
504 = :gateway_timeout
505 = :http_version_not_supported
507 = :insufficient_storage
510 = :not_extended
October 14, 2008 - (>= v2.0.0)
0 thanks

Wrapping peculiarities as of 2.x

In Rails 2.x word_wrap has been improved so that it no longer consumes multiple line-breaks or leading & trailing line-breaks.

word_wrap("\nOnce upon a time\n\nThe End\n")
# => \nOnce upon a time\n\nThe End

However it still doesn’t break long words

# => 30
word_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15)
# => \nOnce upon a\nsupercalifragilisticexpialidocious\ntime
October 14, 2008 - (v1.0.0 - v1.2.6)
1 thank

Wrapping peculiarities

word_wrap will consume multiple line-breaks as well as leading & trailing line-breaks.

word_wrap("\nOnce upon a time\n\nThe End\n")
# => Once upon a time\nThe End

word_wrap will NOT break long words

# => 34
word_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15)
# => Once upon a\nsupercalifragilisticexpialidocious\ntime

If you want a function that will break long words & maintain multiple line-breaks try this alternative. Note it does add a line break at the end of the output.

def breaking_wrap_wrap(txt, col = 80)
  txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,

breaking_wrap_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15)
# => \nOnce upon a\nsupercalifragil\nisticexpialidoc\nious time\n

Regex-based code from http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/

October 11, 2008 - (v2.0.0 - v2.0.3)
1 thank

Assert tag children


#<div id="example">
#  <ul>
#    <li class="btn_1"><a href="#">link1</a></li>
#    <li class="btn_2"><a href="#">link2</a></li>
#  </ul>

assert_tag :tag => 'div',
  :attributes => {:id => "example"},
  :child => {
    :tag => 'ul',
    :child => {
      :tag => 'li',
      :attributes => {:class => 'btn_1'},
      :child => {
        :tag => 'a',
        :attributes => {:href => '#'},
        :content => 'link1'
      :sibling => {
        :tag => 'li',
        :attributes => {:class => 'btn_2'},
        :child => {
          :tag => 'a',
          :attributes => {:href => '#'},
          :content => /link/
October 10, 2008
2 thanks

Make an action onchange

collection_select(nil, :provincia_id, @provincias, :id, :nombre, {:prompt => “Seleccione una provincia”}, {:onchange => “#{remote_function(:url => {:controller => ‘direccions’, :action => ”update_pueblos“}, :with => ”‘provincia_id=’+value“)}”})