Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In rails how can I delegate to a class method

class Task < ActiveRecord::Base   attr_accessible :due_date, :text    def self.this_week     where(:due_date => Date.today.beginning_of_week..Date.today.end_of_week)   end end  class Important < ActiveRecord::Base   attr_accessible :email    has_one :task, :as => :taskable, :dependent => :destroy    delegate this_week, :to => :task end 

So far this delegate is not working as expected, when I try Important.this_week. I get an error saying there is no method this_week defined for class...

Any ideas? Can I even delegate to a class method like this? I may have another class or two extending Task in this way, so I'm curious how this works in a way that doesn't duplicate a bunch of code to each implementing class.

like image 296
peterw Avatar asked Jan 28 '13 17:01

peterw


People also ask

How do you delegate a method in Ruby?

In Ruby, every object inherits from the Object class by default. That's why you get access to methods like puts , class & object_id . With composition a class creates (or is given) objects from another class… then it uses these objects to delegate work to them.

What is delegate method in Rails?

On the Profile side we use the delegate method to pass any class methods to the User model that we want access from our User model inside our Profile model. The delegate method allows you to optionally pass allow_nil and a prefix as well. Ultimately this allows us to query for data in custom ways.

Can a class method call an instance method Ruby?

In Ruby, a method provides functionality to an Object. A class method provides functionality to a class itself, while an instance method provides functionality to one instance of a class. We cannot call an instance method on the class itself, and we cannot directly call a class method on an instance.

What is method delegation?

A delegate method is a method that the delegate object is expected to implement. Some delegate methods are required, while some are not. In IOS, most delegates are expected to conform to an Objective-C protocol; the protocol declaration will tell you which methods are optional and which are required.


1 Answers

You're picking up the ActiveSupport delegation core extension. The delegate helper defines an instance method for the current class so that instances of it delegate calls to some variable on that instance.

If you want to delegate at the class level, you need to open up the singleton class and set up the delegation there:

class Important < ActiveRecord::Base   attr_accessible :email    has_one :task, :as => :taskable, :dependent => :destroy    class << self     delegate :this_week, :to => :task   end end 

But this assumes that Important.task is a reference to the Task class (which it is not)

Rather than relying on the delegation helper, which is just going to make your life difficult, I'd suggest explicit proxying here:

class Important < ActiveRecord::Base   attr_accessible :email    has_one :task, :as => :taskable, :dependent => :destroy    class << self     def this_week(*args, &block)       Task.this_week(*args, &block)     end   end end 
like image 159
Nevir Avatar answered Sep 28 '22 16:09

Nevir