Making instance dependant :has_many scope optional

I was looking to add an instance dependant scope to a model’s attribute has_many configuration:

has_many :sandwiches, -> (user) { where(account_id: user.account_id) }

Unfortunately, this breaks existing usage of of the attribute nested in .includes calls.

Is it possible to define the scope optionally? As in, if an instance is available, use the scope but ignore it if not?

I’ve resolved this by adding an alternative association:

has_many :sandwiches
has_many :sandwiches_alt, -> (user) { where(account_id: user.account_id) }

and adding a overriding getter method so that instances of the model will use the alternative while the origianl association can still be used wherever .includes is used.

def sandwiches