Recent notes

RSS feed
March 18, 2012
0 thanks

Use Join to Turn Array Items into a String.

If you’re looking to take an array like

[ 'don', 'draper' ]

And get

'don draper'

Then use join instead:

[ 'don', 'draper' ].join( ' ' ) 

#=> 'don draper'
March 18, 2012
0 thanks

Destructive to the Original String.

Just as an FYI this function is destructive to the original String object.

name = 'draper' #=> "draper"

name.insert( 0, 'don ' ) #=> 'don draper'

name #=> 'don draper'
March 15, 2012 - (>= v3.2.1)
1 thank

Deprecated proxy_owner

Just change your


calls to


Found it here: http://mileszs.com/deprecation-warnings-for-proxyowner-in-rails

March 10, 2012
2 thanks

flash messages

In rails 3.1 the following does not work for me

redirect_to { :action=>'atom' }, :alert => "Something serious happened" 

Instead, you need to use the following syntax (wrap with parens)

redirect_to({ :action=>'atom' }, :alert => "Something serious happened")
March 5, 2012
0 thanks

Example of conditions using


validates :number, :presence => { :if => :quota_file? }

def self.quota_file?
March 5, 2012
0 thanks

won't refresh updated_at

This will not cause :updated_at column to refresh, while ActiveRecord::Base#increment! would.

February 23, 2012
0 thanks

Be careful with path vs. endpoint

URI.join uses a delimiter – forward slash (/) – to decide if joined strings are a path or endpoint. In order to include strings as part of the path, they must end with a forward slash (/). Otherwise, they are assumed to be an endpoint and are overritten by your new “endpoint”.

Used this way, it (kind of) makes sense:

1.9.2p290 :021 > URI.join("http://localhost/test","main.json")
 => #<URI::HTTP:0x007fa68e81c270 URL:http://localhost/main.json> 

1.9.2p290 :022 > URI.join("http://localhost/test/","main.json")
 => #<URI::HTTP:0x007fa68e80e0d0 URL:http://localhost/test/main.json> 

It is especially confusing when you pass 3 strings and the 3rd (your endpoint) overwrites the 2nd (which you expected to be part of the path).

1.9.2p290 :023 > URI.join("http://localhost/", "test", "main.json")
 => #<URI::HTTP:0x007fa68cec0ba0 URL:http://localhost/main.json> 

1.9.2p290 :024 > URI.join("http://localhost/", "test/", "main.json")
 => #<URI::HTTP:0x007fa68ce14c60 URL:http://localhost/test/main.json> 

Now, consider that you are probably using a variable for the string value of ‘test’.

1.9.2p290 :025 > controller = 'test'
1.9.2p290 :026 > URI.join("http://localhost/", controller, "main.json")
 => #<URI::HTTP:0x007fa68cec0ba0 URL:http://localhost/main.json> 

Your `controller` is simply ignored. Or rather, your endpoint(?) was overwritten.

I’m not sure what versions of ruby this affects. As you can see I am using 1.9.2p290.

February 20, 2012
0 thanks

How to user method 'from'


# => "SELECT `users`.* FROM posts"
February 20, 2012
0 thanks

How to user method from'


# => "SELECT `users`.* FROM posts"
February 15, 2012 - (<= v2.3.8)
0 thanks

@ssoroka and @drova and future readers

I guess these two have already found a solution, but future readers might have not. index and references do not map perfectly

change_table :foo do |t|
  t.references :bar
  t.index      :bar_id

references gets the model name while index gets the column name.

February 15, 2012 - (<= v2.3.8)
0 thanks

Not working in create_table

When using the index method within a create_table statement, it does not have any side effect - at least not in MySQL.

create_table :comment do |t|
  t.belongs_to :post


  # not working inside create_table !
  t.index :post_id

It is working properly in change_table though

change_table :comment do |t|
  t.belongs_to :user

  # this works inside change_table
  t.index :user_id

Unfortunately this flaw is not reported in any way. The index is just not created.

I have only tested this with the mysql2 driver in Rails 2.3.x. I’m not sure, if this happens in other versions/adapters as well.

February 12, 2012 - (v1_8_6_287 - v1_9_2_180)
2 thanks

More Examples


class User < Struct.new(:name, :age, :gender) 

user = User.new("Matz", 43, "male")
February 10, 2012 - (>= v3.1.0)
0 thanks

:defaults no longer work

I’m afraid that :defaults option doesn’t work anymore.

<%= javascript_include_tag :defaults %>

it loads “defaults.js”

Instead, to load all .js files now use

<%= javascript_include_tag "application" %>