如何加入具有 Active Record 中范围的 has_many 关联?

发布于 2024-12-05 09:19:58 字数 693 浏览 3 评论 0原文

这是我的关联和范围设置:

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

这是我得到的错误:

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

da

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2024-12-12 09:19:58

范围可以被视为类方法,关联可以被视为实例方法。在您的代码示例中,owned_lessons 方法返回一个数组对象。您无法在数组对象(或 User 对象)上调用 active_purchase_scope ,因为该范围只能在 Model 类上调用(即在您的情况下 User.active_purchase_scope >)

您可以通过在 Lesson 模型上添加范围来解决此问题

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

,并按如下方式重写 User 类:

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

owned_lessons 返回一个匿名范围Lesson 模型,因此我们可以将其与同一模型中的范围 active_purchase_scope 链接起来。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文