简体   繁体   中英

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

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM