I am trying to figure out if there is a way to reuse a scope from a AR call. Here is my example
@current_account.items.report_by_month(month, year)
Inside the report_my_month scope, I wanted to reuse the @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
This is only sample code to figure out how to do it, because the query is much more complicated as it is a report. Thanks!
Is there a reason that you can't simply pass it as an additional parameter?
def self.report_by_month(month, year, current_account)
values = Values.where(:current_account => current_account)
scope = scoped{}
scope = scope.where(:values => values)
end
Called with
@current_account.items.report_by_month(month, year, @current_account)
Edit:
If you're just trying to avoid having to pass @current_account again, I would suggest adding an instance method on your Account class for this.
class Account
has_many :items
def items_reported_by_month(month, year)
self.items.report_by_month(month, year, id)
end
end
Which you could then call with
@current_account.items_reported_by_month(month, year)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With