A TEXT POST

find_by!

Story.find_by_legacy_id(legacy_id) or raise ActiveRecord::RecordNotFound

You’re doing it really wrong, use the bang version instead, like that: Story.find_by_legacy_id!(legacy_id)

The exception will automatically raised and you don’t need to pollute your code :)

A TEXT POST

Displaying total RAM in MB

You can use free tool:

free -m

or

cat /proc/meminfo

but showing info in KB

http://www.rampant-books.com/t_linux_8_system_buffers_ram.htm

A TEXT POST

Setting default local variables value in Ruby

Complementing this post from Ruby Quicktips

If you want to ensure that a local variable has a value, you can do so:

>> somevar
NameError: undefined local variable or method `somevar' for main:Object
    from (irb):1
>> somevar = somevar || "somevalue"
=> "somevalue"

but if you try to set another variable, using a value of a variable that may not exists, you will get an error:

>> somevar
NameError: undefined local variable or method `somevar' for main:Object
    from (irb):1
>> someothervar = somevar || "somevalue"
NameError: undefined local variable or method `somevar' for main:Object
    from (irb):2
A TEXT POST

Finding removed code in git

If you need to find out what commit a word/phrase was removed, you may try this:

git log --pretty=oneline -S'my_method_name'

This command can be used to find any piece of code, removed or not.

A TEXT POST

Show full diff with git stash

By default git stash show shows only a simple diff, to show the full version, use -u parameter, like this:

git stash show -u
A TEXT POST

Rails, thin and rack 1.1.0

if you are trying to use thin with a rails app and is getting this:

$ thin start
>> Using rails adapter
Missing the Rails 2.3.5 gem. Please `gem install -v=2.3.5 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.

so, all you need to do is uninstall rack 1.1.0. I am using rack 1.0.1 now it’s working very nice.

source: http://osdir.com/ml/RubyonRailsTalk/2010-01/msg01970.html

PS: Could someone, please, show me a theme that no breaks with long lines of code? ¬¬

A TEXT POST

String conversions using ActiveSupport

ActiveSupport adds some methods to convert strings into dates/times, it’s very nice, take a look.

Instead:

>> Date.parse('2010-01-12')
=> Tue, 12 Jan 2010

You can do:

>> '2010-01-12'.to_date
=> Tue, 12 Jan 2010

It’s very more intuitive and readable!

http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Conversions.html