Internetbureau Holder

nifty rails 3.0 methods II

Daniel Willemse zo 27 jun 10

@Wijnand,
I have to agree with you, in that class methods you describe can do the same the scopes can. The only reason I can think of, is the one you would reject.

Now for the new methods I made a post on about a week ago.
I thought to myself, what would happen if i wrote a class method “order” and tried to use it on the class.
Here’s what I tried:

 
Class Invoice < ActiveRecord::Base
  def self.order
   return "new class method"
  end
end

Now, when I tried this in the console
with

>> Invoice.order

I got the expected “new class method” string returned.

With

>> Invoice.order.where("id = ?", 1)

I got
NoMethodError: undefined method `where’ for new class method

Which was not unexpected.

However, when I tried Invoice.where(“id = ?”, 1).order
It returned the active record relation as if ignoring my own class method.

Keep this in mind when (for whatever reason you may have) overriding
certain class methods like order or where. Should you want to use your order method over the pre-defined rails one, you need to put that method first in line on your model like
Model.own_order_method.where(“arguments”)

Gepost in hor |  0 reacties

Plaats je reactie





Welcome to Holland On Rails

This weblog is the official Ruby techblog from the guys at Holder, a Ruby development company. Holder is also the company behind the RubyAndRails Europe Conference in Amsterdam.

Recente Jobs


Bekijk alle jobs »»

Gereedschapskist

Onmisbare tools voor
iedere developer!
Ruby On Rails
Framework voor de web 2.0 developer. Eindelijk vooruitgang!
TextMate
Editor for true pro's
Typ, tab, top :-)
Nee, niet voor Win.
Made On A Mac
En nou is het over met die saaie grijze Windows bak van je!

Auteurs op deze site

Chris Obdam

'Less is more' evangelist, past dit ook dagelijks toe op zijn tandenborstel.

Chiel Wester

Snelheidswonder op Ruby wielen. Leuk om mee te pair-programmen ;-) Recommend Me

Stephan Kaag

Het eerste Rails coreteam- member uit Nederland? Rails evangelist van het eerste uur.

Paul Engel

Én Rails programmeren én interfaces designen? Je zou hem superman kunnen noemen..

Dax Huiberts

Official Zip-Programmer, skinny code is helemaal zijn ding. Haalt meer code weg dan dat er bij komt.

Freek Monteban

Het nieuwste telg uit het Holland on Rails nest! Hij doet niets anders meer!

Johan Vermeulen

De stylesheet-koning uit de kop van Noord-Holland!