Recent notes
RSS feed

@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.

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.

More Examples
Code
class User < Struct.new(:name, :age, :gender) end user = User.new("Matz", 43, "male")

: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" %>

: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


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`

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

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

Example Usage
End of Day for Any Date
DateTime.new( 2011, 01, 01 ) # => Sat, 01 Jan 2011 00:00:00 +0000 DateTime.new( 2011, 01, 01 ).end_of_day # => Sat, 01 Jan 2011 23:59:59 +0000
With Local Timezone
DateTime.civil_from_format( :local, 2011, 01, 01 ).end_of_day # => Sat, 01 Jan 2011 23:59:59 -0700

Stubs Logger in rspec
Let we have a module like below:
module MyModule class << self def logger @logger ||= Logger.new(File.join(Rails.root, "log", "my_gem_#{Rails.env}.log")) end end end
To use this logger just type:
MyModule.logger.info "This is a log line"
To stub in tests use (for rspec):
require 'active_support/log_subscriber/test_helper' RSpec.configure do |config| config.include ActiveSupport::LogSubscriber::TestHelper config.before do MyModule.stub!(:logger).and_return(MockLogger.new) end end
Usefull in testing when you don’t like to log anything.

Example
Check if id column exists in users table
ActiveRecord::Base.connection.column_exists?(:users, :id)

Pluralize with Text.
Example
1 person or 3 people
Use a View Helper
pluralize( 1, 'person' ) # => 1 person pluralize( 2, 'person' ) # => 2 people # In practice. pluralize( Person.count, 'person' )
See
http://apidock.com/rails/ActionView/Helpers/TextHelper/pluralize

Options
I came across the following situation An article has a history of friendly url being that the foreign key that represents the value of the article’s id in the table is called Friend url_id then in that case:
Article.joins(“INNER JOIN friends ON articles.id = friends.url_id”).where(“friends.url like ? ”, url)
if the column url_id was renamed for artigo_id would be easier
Article.joins(:friend).where(“friends.url like ? ”, url)

link_to with :as routing
The following will not work when your post model is routed with the :as option:
link_to("View post", @post)
Instead, use the helper with your custom name:
link_to("View post", :url => renamedpost_path(@post))

form_for with :as routing
The following will not work if your post model is routed with the :as option:
form_for(@post)
Instead, use the helper with your custom name:
form_for(@post, :url => edit_renamedpost_path(@post))

Model objects routed with :as
When providing a model object, url_for will not work if the model’s routes are named using the :as option. You can instead use the named helper methods (posts_path, post_path(:id), etc.).

Total Unique Elements: Set Union
For total unique elements, see set union: http://apidock.com/ruby/Array/|

with a params constant
If you want to have a params with the same value on all of the urls in this namespace, you can write this :
with a constant param :admin set to true
namespace :admin, :admin => true do resources :posts end
all of the urls like /admin/post have a param :admin with the value true.
It works also with :
scope 'admin', :admin => true do ... end match 'administration', :admin => true => 'posts#index' get 'administration', :admin => true
etc…

Set ids when using a collection of values (cont.)
Concerning greeneggs614’s post:
The following version would be a bit more intention revealing while providing the same output:
<% Car.each do |c| %> <%= check_box_tag "car_ids[]", c.id, :id => "car_ids_#{c.id}" %> <% end %>


How to include a “Please select…” (default/prompt) in a grouped dropdown list.
The clue here is that you actually specify the prompt on the select element, and not in the option_groups_from_collection_for_select element.
<%= f.select :post_type_id, option_groups_from_collection_for_select(@categories, :post_types, :name, :id, :name), :include_blank => "Please select..." %>
Hope this helps someone.

Total Unique Elements from Two Arrays
Simple but thought it was worth mentioning:
( [ 1, 2, 3 ] + [ 3, 4, 5 ] ).uniq #=> [ 1, 2, 3, 4, 5 ]


Links and basic explanation
This function, available on any Base derived class, allows eager loading. So when iterating over a result, all the data can be pre-cached, reducing the number of queries from 1+x to 2.
Several ways to use this include:
Post.includes(:author).where(..)
Post.includes([:author, :comments]).where(..)
Post.includes( :comments => :replies ).where(..)
The result is a Relation, so where and other function may be called.
.includes(:comments).to_a is the same as , .find(:all, :includes => :comments)
Some good documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Apperently one can get similar results by using joins, but i’m not expert on joins, so please ammend and read: http://guides.rubyonrails.org/active_record_querying.html

Recommendations
I would just use the path_to_image. I find that this is what works best. As it says above it can create problems.
Here is my code
Code example
def background_path(background) if background path_to_image background.file_name.normal else path_to_image "background_preview.jpg" end end def flavor_path(flavor) if flavor path_to_image flavor.file_name.normal else path_to_image("flavor_preview.jpg") end end
basic but gets the job done and it does not have problem with my pre built paths which are called image_path

See also: sample
sample randomly picks 1 or n elements from the array

Use kill 0 to find out if process is running
is_running.rb:
#!/usr/bin/env ruby pid = ARGV[0].to_i begin Process.kill(0, pid) puts "#{pid} is running" rescue Errno::EPERM # changed uid puts "No permission to query #{pid}!"; rescue Errno::ESRCH puts "#{pid} is NOT running."; # or zombied rescue puts "Unable to determine status for #{pid} : #{$!}" end
Thanks to http://stackoverflow.com/a/200568/51209

example
[‘one’,‘two’,‘three’].to_sentence # => “one, two, and three”