我试图弄清楚是否有一种方法可以重用AR调用中的作用域。下面是我的例子
@current_account.items.report_by_month(month, year)在report_my_month作用域中,我想重用@current_account
def self.report_by_month(month, year)
values = Values.where(:current_account => USE SCOPE FROM SELF)
scope = scoped{}
scope = scope.where(:values => values)
end这只是一个示例代码,用于确定如何执行此操作,因为查询要复杂得多,因为它是一个报告。谢谢!
发布于 2012-08-12 11:38:31
有没有什么原因不能简单地将其作为附加参数传递?
def self.report_by_month(month, year, current_account)
values = Values.where(:current_account => current_account)
scope = scoped{}
scope = scope.where(:values => values)
end使用调用
@current_account.items.report_by_month(month, year, @current_account)编辑:
如果你只是想避免再次传递@current_account,我建议在你的Account类上添加一个实例方法。
class Account
has_many :items
def items_reported_by_month(month, year)
self.items.report_by_month(month, year, id)
end
end然后,您可以使用
@current_account.items_reported_by_month(month, year)https://stackoverflow.com/questions/9815206
复制相似问题