Recent notes

RSS feed
November 15, 2011 - (v3.1.0)
0 thanks

Wrapping with select tag

I didn’t knew how to wrap the output with <select> tag. I didn’t want to use raw html, but the doc doesn’t mention another way.

So, here is what I tried and it’s working:

<%= f.select :entry, option_groups_from_collection_for_select(@categories, :entries, :name, :id, :name) %>

I hope this helps anyone. :-)

November 14, 2011 - (v3.1.0)
2 thanks


For Rails 3.1 check see:

http_basic_authenticate_with :name => "username", :password => "pass" 
November 12, 2011 - (v2.0.0 - v2.3.8)
0 thanks

Specify attachment names

If you want to give your attachment a name, you can do this:

attachment :filename => 'my_file.txt', :body => File.read('/var/null')

It will appear to the recipient as a file named “my_file.txt” rather than something awful like “noname 1”.

November 11, 2011
3 thanks

Catching and throwing -- don't!

@wiseleyb and @glosakti, neither of your suggestions are necessary, and both are bad practices.

This test:

test "transactions" do
  assert_raises ZeroDivisionError do
    User.transaction do

passes just fine on its own, with the transaction rolled back as you’d expect. No need to hack something ugly together.

November 10, 2011
0 thanks

logic in class/id

If you need to place some simple logic in class or like that, I think that best to make it with simple brackets:

Code example

<%= link_to ‘All actions’, switch_action_tab_path, :remote => true, :class => (‘selected’ if @tab == ‘all’) %>

November 7, 2011 - (v3.0.5 - v3.0.9)
1 thank

unscoped.all / unscoped.count

At least in console, doing unscoped.all or unscoped.count initially returns expected results but, after you’ve added new records outside of the default_scope those two calls seem to use some cached values.

Therefore it should always be used with the block (as they sort of imply in the doc). unscoped { all } and unscoped {count }

November 6, 2011 - (>= v3.1.0)
3 thanks

Removed in 3.1.x

This method (and #auto_link_urls) has been removed in Rails 3.1 - other options are out there, such as Rinku, however there is a gem you can use for migration purposes etc, which is rails_autolink: http://rubygems.org/gems/rails_autolink

October 26, 2011
2 thanks

Adding to the URL

If you want to use polymorphic routing for your object but you also need to specify other stuff like an anchor, you can explicitly generate the polymorphic url with extra options:

form_for @candidate,
  :url => polymorphic_path(@candidate, :anchor => 'signup')
October 19, 2011 - (>= v3.0.0)
3 thanks

Replaced by :on => :create

From rails 3,


has been removed and replaced with:

before_validation :foo, :on => :create
October 18, 2011
0 thanks

Upgrading to 3.x


Since this is deprecated, one can watch the Railcast for upgrading to 3.x

The equivalent is the ActiveRecord finder methods. http://apidock.com/rails/ActiveRecord/Fixture/find

October 10, 2011 - (>= v3.1.0)
1 thank

Use Ruby instead!


class TestClass < SomeSuperClass
  attr_accessor :sample_acc

  def initialize      
    @sample_acc = []

If nil is not a valid value for this accessor, then you can just define reader for it.

class TestClass
  attr_accessor :sample_acc

  def sample_acc
    @sample_acc ||= 98
October 8, 2011
6 thanks

Undocumented :location option

You can use undocumented :location option to override where respond_to sends if resource is valid, e.g. to redirect to products index page instead of a specific product’s page, use:

respond_with(@product, :location => products_url)  
October 7, 2011
3 thanks

How to submit current url

For example to change some kind of param on select change…

<%= form_tag({}, {:method => :get}) do %>
  <%= select_tag :new_locale, options_for_select(I18n.available_locales, I18n.locale), :onchange => "this.form.submit();" %>
<% end %>
October 6, 2011
1 thank

Difference in the way returns are handled

Also, there is a difference in the way returns are handled from the Proc. A return from Proc.new returns from the enclosing method. Return in lambda-block acts like in regular method.

return example

def proc_return
  Proc.new { return "Proc.new"}.call
  return "proc_return method finished"

def lambda_return
  lambda { return "lambda" }.call
  return "lambda_return method finished"
puts proc_return
puts lambda_return
# => Proc.new
# => lambda_return method finished
October 4, 2011
1 thank

Unexpected rounding behavior

Both 2.5 and 1.5 are rounded to 2…

ree-1.8.7-2010.02 > sprintf("%.f", 0.5) 
=> "0" 
ree-1.8.7-2010.02 > sprintf("%.f", 1.5)
=> "2" 
ree-1.8.7-2010.02 > sprintf("%.f", 2.5)
=> "2" 
ree-1.8.7-2010.02 > sprintf("%.f", 3.5)
=> "4" 
ree-1.8.7-2010.02 > sprintf("%.f", 4.5)
=> "4" 

use round instead to get proper behavior

October 4, 2011
2 thanks

How to change format automatically depending on locale...

… without passing locale option.

In your application_helper.rb (or in other helper) place following code:

def number_to_currency(number, options = {})
  options[:locale] ||= I18n.locale
  super(number, options)

Then, in your locale files:

        format: "%n %u"
        unit: "USD"

And that is it :)

October 3, 2011 - (>= v1_9_1_378)
0 thanks

example will not work in 1.9+

Since 1.9 introduces native threads, we cannot assume the order of exectution and the example above is not thread safe and fails with a “deadlock detected (fatal)” error. Also Thread.pass is pointless in the context of native threads.

This will work as intended with native threads:

a = Thread.new { print "a"; Thread.stop; print "c" } 
sleep(0.1) until a.status == 'sleep'
print "b"
October 3, 2011
0 thanks


If you’re actually looking to cut down on code why not use .map instead of the longer .collect?

September 30, 2011 - (>= v3.0.0)
1 thank

Using a block with image_tag

HTML5 officially supports block-level elements in the anchor tag and Rails 3 allows you to pass a block to image_tag:

<%= image_tag(some_path) do %>

<%= content_tag(:p, "Your link text here")

<% end %>

September 30, 2011 - (v3.0.0 - v3.1.0)
0 thanks

Without module

If you want to have only the path prefix without namespacing your controller, pass :module => false.


namespace :account do
  resources :transactions, :only => [:index]

account_transactions GET /account/transactions(.:format)
{:controller=>"account/transactions", :action=>"index"}

With :module => false:

namespace :account, :module => false do
  resources :transactions, :only => [:index]

account_transactions GET /account/transactions(.:format)
{:controller=>"transactions", :action=>"index"}
September 29, 2011 - (v2.0.0 - v3.1.0)
2 thanks

Additional Format meaning

%e - Day of the month, without leading zero (1..31)

September 27, 2011 - (>= v3.1.0)
0 thanks

1.9 behavior

“In Ruby 1.9 and newer mb_chars returns self’”

This would seem to be a lie. At least in rails 3.1.0 and ruby 1.9.2, mb_chars still returns a proxy object with additional useful methods defined on it that aren’t on a 1.9.2 String.

ruby-1.9.2-p180 :007 >  "àáâãäå".normalize(:kd)
NoMethodError: undefined method `normalize' for "àáâãäå":String

ruby-1.9.2-p180 :008 > "àáâãäå".mb_chars.normalize(:kd)
 => àáâãäå
September 25, 2011
1 thank

Parsing the Date Params into a New Date Object.

Useful for when you need to create a Date object from these manually, such as for reporting.

If the date select input is start_date and it belongs to the report form object:

@start_date = Date.civil(params[:report]["start_date(1i)"].to_i,

# => Date

# => Sun, 25 Sep 2011  # For example.

Use a similar method for DateTime situations.

September 17, 2011
0 thanks

this works, but doesn't make sense

does it?

replace(klass.find(ids).index_by { |r| r.id }.values_at(*ids))



I see no point in making it that complicated

September 15, 2011
1 thank

beware of trying to dup in subclass inside class context

The example of adding to an array without effecting superclass:

# Use setters to not propagate changes:
Base.setting = []
Subclass.setting += [:foo]

That’s right as far as it goes. But beware when you are in context of class definition:

class Subclass < Base
   # possibly wrong, ruby seems to get 
   # confused and think you mean a local 
   # var, not the class ivar
   setting += [:foo]

   # But this will work:
   self.setting += [:foo]

   # Or:
   self.setting = self.setting.dup
   self.setting << :foo

September 14, 2011
0 thanks

Find a Selected Option in a Drop Down.

You can find the selected option (or options for multiple select inputs) with the following:

assert_select('select#membership_year option[selected]').first['value']
September 14, 2011
2 thanks

More on add_to_base

Actually, use

model_instance.errors.add :base, :invalid

to have I18n working.