In a screen cast on Exporting CSV from a rails app, Ryan Bates presented the following simple code.
I’m trying to figure out how the class method Product::to_csv actually gets invoked on line 5 of ProductController.rb, as it doesn’t seem to follow the normal Ruby rules.
product.rb
1 class Product < ActiveRecord::Base
2 def self.to_csv(options = {})
3 ...
4 end
5 end
products_controller.rb
1 class ProductsController < ApplicationController
2 def index
3 @products = Product.order(:name)
4 respond_to do |format|
5 format.csv { send_data @products.to_csv }
6 ...
7 end
8 end
9 end
Since to_csv is a class method, I’d expect the invocation to look like Product::to_csv().
According to the documentation, @products is an instance of ActiveRecord::Relation. Why do messages sent to an instance of ActiveRecord::Relation cause methods on the Product class object to get invoked? To make it even stranger, renaming to_csv to some arbitrary name in both the the sender and receiver leads to NoMethodError, so maybe there is some magic afoot based on names that begin with to_?
Am I missing something obvious? Any clarification would be greatly appreciated.
This is just one of the things Rails does. Any class methods automatically become available as “collection” methods, meaning they are available to the relation objects. Scopes and class methods are interchangeable that way.