pixelearth
pixelearth

Reputation: 14620

How can I join a has_many association with a scope in Active Record?

This is my association and scope setup:

has_many    :owned_products

has_many    :owned_lessons, :through => :owned_products, :source => :lesson, :conditions => "owned_products.product_type = 'Lesson'"

scope :active_purchase_scope, lambda {
    where('owned_products.created_at' => (Date.today - CONFIG['downloads']['time_window'].days)..(Date.today)).order('owned_products.created_at DESC')
}

def active_lessons 
    owned_lessons.active_purchase_scope
end

This is the error I get:

ruby-1.8.7-p334 :005 > User.find_by_username('joeblow').active_lessons
NoMethodError: undefined method `active_purchase_scope' for #<User:0x1051a26c8>

da

Upvotes: 2

Views: 1570

Answers (1)

Harish Shetty
Harish Shetty

Reputation: 64363

A scope can be treated as a class method and an association can be treated as an instance method. In your code sample, the owned_lessons method returns an array object. You can't call active_purchase_scope on the array object (or User object for that matter) as the scope can only be invoked on the Model class (i.e. in your case User.active_purchase_scope)

You can address this issue by adding the scope on the Lesson model

class Lesson
  has_many    :owned_products

  scope :active_purchase_scope, lambda {
    include(::owned_products).where('owned_products.created_at' => 
          (Date.today - CONFIG['downloads']['time_window'].days)..(Date.today)).
            order('owned_products.created_at DESC')
  }

end

And rewrite the User class as follows:

class User

  has_many    :owned_products

  has_many    :owned_lessons, :through => :owned_products, :source => :lesson, 
                 :conditions => "owned_products.product_type = 'Lesson'"


  def active_lessons 
    owned_lessons.active_purchase_scope
  end

end

The owned_lessons returns an anonymous scope on the Lesson model, hence we can chain it with the scope active_purchase_scope from the same model.

Upvotes: 4

Related Questions