Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...