Flowdock

Recent notes

RSS feed
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

f.e.

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

def self.quota_file?
  quota_file?
end
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'

example

User.from('posts').to_sql
# => "SELECT `users`.* FROM posts"
February 20, 2012
0 thanks

How to user method from'

example

User.from('posts').to_sql
# => "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
end

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

  t.timestamps

  # not working inside create_table !
  t.index :post_id
end

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
end

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

Code

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

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" %>
February 9, 2012 - (v1.2.0 - v3.1.0)
0 thanks

:only, :except and passing in multiple parameters

To specify that the filter should be applied to or excluded from given controller actions, use the :only and :except parameters. To pass in multiple controller actions use an array:

after_filter :authorize, :except => [:index, :show]
after_filter :authorize, :only => :delete

Stolen from: http://apidock.com/rails/ActionController/Filters/ClassMethods/before_filter

February 9, 2012 - (v2.3.2 - v3.1.0)
0 thanks

reject

This is inverse operation of select. If block return false add item to array.

[1,2,3,4].reject {|n| n%2==0}

> [1, 3]

Like select method with inverse.

January 31, 2012 - (v3.0.0 - v3.1.0)
0 thanks

undocumented events/symbol/types

@nessur is right: drop the “before_” and “after_” prefixes and you have the possible values for the uninformatively named `symbol` param: create, save, update and validate.

So to remove all validations for a model do: `reset_callbacks :validate`

January 31, 2012
1 thank

To use with factory_girl and prevent leaking file handles

As insane-dreamer noted, to use with factory_girl:

Factory :video_file do
  file { fixture_file_upload 'test.png', 'image/png' }
end

However, I ran into an issue where one of our spec’s was creating a few hundred files and would then crash with:

Errno::EMFILE: Too many open files

If you look at the source code for fixture_file_upload, it creates a new file. I don’t know if these files were being leaked, or just not getting garbage collected, but they never got closed. Eventually they just piled up and the process hit the max open file limit.

So, you need to explicitly call close on the file handle returned from fixture_file_upload. However, you can’t call close in the block immediately after fixture_file_upload because the factory needs to be able to read from the stream in order to properly initialize ‘file’.

Instead what I had to do was perform the close on the ‘proxy’ in an after_create block, like so

Factory :video_file do
  file { fixture_file_upload 'test.png', 'image/png' }

  after_create do |video, proxy|
    proxy.file.close
  end
end
January 30, 2012
0 thanks

Requires a Block.

Just a little heads up here because it’s not obvious.

This requires a block to be passed to it.

Example Usage

say_with_time "Reverting all service rates to nil." do
  Service.update_all( :rate, nil )
end

# Output
-- Reverting all service rates to nil.
   -> 0.3451s
   -> 2233 rows