Class methods in Ruby on Rails 3 — I'm totally lost?

Class methods in Ruby are really just members of the singleton class, and doing class Do_more is equivalent to: tmp = foo. Do_something bar = tmp. Do_more In Rails, this chainability is most often used for building SQL queries (e.g. , with where or order etc. ).

This is achieved because each of these methods returns an ActiveRecord Relation The reason foo.scoped. My_foo_class_method works is because of ActiveRecord::Relation#method_missing doing the following: elsif @klass. Respond_to?(method) scoping { @klass.

Send(method, *args, &block) } Which checks if the ActiveRecord class responds to the method called, and if so, calls that.

Class methods in Ruby are really just members of the singleton class, and doing class For example: bar = foo. Do_something. Do_more is equivalent to: tmp = foo.

Do_something bar = tmp. Do_more In Rails, this chainability is most often used for building SQL queries (e.g. , with where or order, etc.). This is achieved because each of these methods returns an ActiveRecord Relation.

The reason foo.scoped. My_foo_class_method works is because of ActiveRecord::Relation#method_missing doing the following: elsif @klass. Respond_to?(method) scoping { @klass.

Send(method, *args, &block) } Which checks if the ActiveRecord class responds to the method called, and if so, calls that.

1AndrewMarshall +1 Thank you for the answer. I understand what would makes a method "chainable"; I don't understand how Rails takes a class method and makes it chainable. – ClosureCowboy Mar 9 at 4:48 That leading 1 is beautiful.

– ClosureCowboy Mar 9 at 5:00 Ooohhhhhh I think I see what you mean. Are you asking why a named scope specific to an individual model is chainable with the usual where, order, etc.? (If not, please clarify a little more by editing your question with a specific example, thanks.) – Andrew Marshall Mar 9 at 5:52 I'm actually unsure if something like User.order. By_author would work… though User.

By_author. Order definitely would. – Andrew Marshall Mar 9 at 5:57 It does.

My best guess is that it uses Ruby's fantastic method_missing to look for the method in the model class, but I'd have to dive into the code to be sure. – Andrew Marshall Mar 9 at 6:01.

Having class Instead of calling def self. My_method1 end def self. My_method2 end class.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions