Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Access Scoping Chain to reuse

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!

like image 659
bokor Avatar asked Nov 19 '25 09:11

bokor


1 Answers

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)
like image 158
sgrif Avatar answered Nov 22 '25 01:11

sgrif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!