Reputation: 524
I'm currently building a query like this:
account.sales.method_needing_more_account_info(account)
I want to be able determine account based on the scope that is already there, simplifying the method calls to something like this:
account.sales.method_pulling_account_from_scope
I'm doing this as the account model holds certain settings that determine how sales data is presented and alter the queries to match.
Upvotes: 3
Views: 2813
Reputation: 4332
Scopes are class methods and therefore unaware of instance variables and attributes, in order to do what you are asking you have to stick to your first code sample.
The alternative would be to write a method that returns the data
Rails 2
# returns an array of results
def more_account_info
self.sales.all(:conditions => [])
end
# i've also added an initializer to allow for returning a scope before
# details here: http://railscasts.com/episodes/112-anonymous-scopes
class ActiveRecord::Base
scope :conditions, lambda { |*args| {:conditions => args} }
end
# which allows for me to return a scoped object (chainable)
def more_account_info
scope = Account.scoped({})
scope = scope.sales
scope = scope.conditions ""
end
or in rails 3 you could return an Arel object (chainable)
Rails 3
# returns an arel object rather than an array of results
def more_account_info
self.sales.where("")
def
Upvotes: 3