Notes posted by Vidmantas
RSS feed
Pass a block
While this example is not so obvious on first look what the block passed does, here’s a small explanation:
when the block is passed to this function, the uniqueness is checked based on a value returned by that block.
For example if it’s array of objects with “user_id” method, then this would be:
tasks.uniq{|t| t.user_id } # returns only tasks with unique user_id

Look at mail gem
If you’re looking for mailer methods like “deliver” for rails 3 or later, you should look into “mail” gem: http://github.com/mikel/mail

Works with scoped too
It’s also available to use after scope chain too, like in any other AR action, for example:
User.where('age > 69').delete_all

If on Rails 3
If you’re on Rails 3, you should look into
http://apidock.com/rails/ActiveRecord/Relation/update_all

Default fallback
You can specifly :default option which is useful when the translation is not found. For example:
t(:this_translation_doesnt_exist, :default => 'Ooops!') # => Ooops!
Or even any number of “fallbacks” - the first not nil is returned:
t(:missing, :default => [:missing_too, :existing, 'Sad panda']) # => :existing translation
Good introduction to Rails I18n is http://guides.rubyonrails.org/i18n.html

How to set default value to NULL
To set default value to NULL you can use change_column method instead, for example:
change_column :suppliers, :qualification, :string, :default => nil
Just make sure you don’t change data type accidentally ;-)

Do not forget to add indexes
Don’t forget to add indexes to HATM table:
add_index :developers_projects, [:developer_id, :project_id]

Example
This function can be used to pass the ID of selected item, for example:
# with select or collection_select helpers: { :onchange => remote_function(:url => { :action => 'do_smth' }, :with => "'id=' + $('the_id').value") } # and grab ID in controller action as usually: YourModel.find(params[:id])

Common options
“Common options” mentioned here is default PrototypeHelper options documented in link_to_remote
This means you can use :loading, :loaded, :failure, :success, etc in observe_field.

Not implented yet
According to this method’s source, change_column_default is not implemented as well as change_column

If you're not using resource
If you don’t use resource for your remote_form_for, then :url option is necessary.
For example:
<% remote_form_for "not_resource" do |f| ... %>
won’t work. But with :url option, it will:
<% remote_form_for "not_resource", :url => { :controller => "recommend", :action => "send" } do ... %>

Rake tasks for gem dependencies
You can manage installation and other tasks for these dependencies with rake tasks, for example:
rake gems:install # Installs all required gems for this application rake gems:unpack # Unpacks the specified gem into vendor/gems
To get all rake tasks about gems:
rake -T gems

Update element after remote call
Not mentioned in the documentation, you can add :update option to the remote_form_for and pass the id of element you’d like to update after ajax action as you do with link_to_remote, for example:
<% remote_form_for "comment", :update => "form" } do |f| %> # your form here <% end %>
Or
<% remote_form_for "comment", :update => {:success => "form", :failure => "errors"} do |f| %> # your form here <% end %>

Examples
@articles = cache(‘articles’) do
Articles.latest
end
Or:
@articles = cache([‘articles’, user.id], :expires_in => 15.minutes)
# Advanced Rails Recipies says: "expires_in option works only with memcached store" Articles.latest
end
Also if you’d like to have short ‘cache’ call in your model you can have it - add the following method to the model:
def cache(key, options = {})
ActionController::Base.cache_store.fetch(key, options) { yield }
end

Multipart form
Don’t forget to add :multipart => true if you have file upload in your form.
<% form_for "user", :html => { :multipart => true } do |f| %>

You can turn off dirty objects
If you expierence problems with dirty objects you can turn it off:
ActiveRecord::Base.partial_updates = false

Moved to plugin "auto_complete"
This helper was moved to plugin “auto_complete” - http://svn.rubyonrails.org/rails/plugins/auto_complete/

Multiple filter methods with :only, :except
Notice that this methods accepts *filters param, so you can pass array of methods with :only or :except too
Example
before_filter [:authorize, :set_locale], :except => :login

2.1 sets UTC time by default
Rails 2.1 sets hour select to UTC time value, not local server time by default. So if you’re not in UTC time zone don’t forget to specify timezone in your config/environment.rb: config.time_zone = 'Vilnius'