Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

how does this Ruby class method get invoked?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...