Some Rails / Ruby tricks you should know 1

Posted by kain Mon, 25 Aug 2008 04:17:00 GMT

Grab column names for a model with magic fields and primary key stripped out

>> Dictionary.content_columns.collect { |col| col.name }
=> ["it", "en"]
# or
>> Dictionary.content_columns.map(&:name)
=> ["it", "en"]

Get an ActiveRecord Model attributes

>> Dictionary.first.attributes
=> {"it"=>"ciao", "id"=>1, "en"=>"hello"}

Discover which column yields the data you’re looking for

>> Dictionary.first.attributes.index("hello")
=> "en"

Take out some given keys from an Hash easily

# I think rails should need a method similar to content_columns and attributes_names but for extracting content_values
>> Dictionary.first.attributes.except("id")
=> {"it"=>"ciao", "en"=>"hello"}

Remove all whitespace on both ends of a string, change multiple spaces into one

" foo   bar    \n   \t   boo".squish # => "foo bar boo"

Manage repetitive find conditions via a Hash

# Basic
>> Model.find(:all, :conditions => ['first_name LIKE :name OR last_name LIKE :name', {:name => "aname"}])
# Extended example
# available_languages is a method which returns an Array.
>> word = "hello"
>> conditions = [ Dictionary.available_languages.collect { |language_column| language_column + ' = :word' }.join(" OR "), { :word => word } ]
>> Dictionary.find(:first, :conditions => conditions)

Not really a rails feature but you can append methods to the "end" block

    query_string_array.inject('') do |string_so_far, current_word|
      string_so_far + ' ' + lookup_word_and_build_translation_string(current_word)
    end.squish

Returns an array with the names of the subclasses of a class

# This works in production mode or development with config.cache_classes =  true or when classes are loaded
>> Account.send(:subclasses).map { |o| o.to_s } # this is protected in AR
# or
>> Object.subclasses_of(Account).map { |o| o.to_s }

Build up a sentence from an array taking advantage of the new I18n rails adapter, without last comma

# This will also use the "connector" word you defined in your language, default for en-US is "and".
>> %w(a b c).to_sentence(:skip_last_comma => true)
=> "a, b and c"
# Add in your language file:

  # Used in array.to_sentence.
  support:
    array:
      sentence_connector: "e"

There’s a ton of other stuff, just browse Active Support files to find much more useful and time-saving extensions. Now go refactor your code and make it even more prettier :)