Notes posted by foliosus
RSS feed
On destroying data
Reply to tvle83 and pgmcgee: the destructiveness of this method depends on your database. Some databases are better at converting between disparate types than others. For example, when changing a column from a numeric type to a string type, some databases drop the data where others will turn the numbers into their string representations.
Essentially, YMMV.

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

Loop through matches of your regular expression-based search
If you want to loop through the matches of a regular expression, String#scan is your friend. You can do something like this:
matches = '12345'.scan(/\d/) matches.each do |m| puts m end # => 1 # => 2 # => 3 # => 4 # => 5

Common AJAX options
See the documentation for link_to_remote to see the common AJAX options, like :before and :completed.

Looking for the docs?
Check the ClassMethods – the docs on filters are there.

The docs are in the base class
Look in ActionController::Base for the docs.


The docs are in AR::Base
The docs you’re looking for are in ActiveRecord::Base

Use helpers in your ActionMailer views
It’s very easy to give your mailer access to helpers:
# Let your mailer user the ApplicationHelper methods class MyMailer < ActionMailer::Base helper :application end

Use the current URL, with changes
You can use the current URL, whatever it is, with changes, as in:
# Create a link to the current page in RSS form url_for(:overwrite_params => {:format => :rss})
This can be super-helpful because it preserves any GET params (like search parameters)

You can't have many :through with habtm
Imagine the following
a has_many b b has_and_belongs_to_many c a has_many c :through => b
a.b works fine
b.c works fine
a.c throws an error!
has_many :through where the through association is a habtm is not supported in Rails. The error is:
ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_and_belongs_to_many for has_many :stories, :through => :subcategories. Use :source to specify the source reflection
Specifying the source reflection still won’t help you though, because this kind of has_many :through isn’t supported at all.