首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 3-要重用的访问作用域链

Rails 3-要重用的访问作用域链
EN

Stack Overflow用户
提问于 2012-03-22 09:21:37
回答 1查看 425关注 0票数 3

我试图弄清楚是否有一种方法可以重用AR调用中的作用域。下面是我的例子

代码语言:javascript
复制
@current_account.items.report_by_month(month, year)

在report_my_month作用域中,我想重用@current_account

代码语言:javascript
复制
def self.report_by_month(month, year)
    values = Values.where(:current_account => USE SCOPE FROM SELF)
    scope = scoped{}
    scope = scope.where(:values => values)
end

这只是一个示例代码,用于确定如何执行此操作,因为查询要复杂得多,因为它是一个报告。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2012-08-12 11:38:31

有没有什么原因不能简单地将其作为附加参数传递?

代码语言:javascript
复制
def self.report_by_month(month, year, current_account)
    values = Values.where(:current_account => current_account)
    scope = scoped{}
    scope = scope.where(:values => values)
end

使用调用

代码语言:javascript
复制
@current_account.items.report_by_month(month, year, @current_account)

编辑:

如果你只是想避免再次传递@current_account,我建议在你的Account类上添加一个实例方法。

代码语言:javascript
复制
class Account
  has_many :items

  def items_reported_by_month(month, year)
    self.items.report_by_month(month, year, id)
  end
end

然后,您可以使用

代码语言:javascript
复制
@current_account.items_reported_by_month(month, year)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9815206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档