I am attempting to use inheritance from a class which has a named scope:
Class A < ActiveRecord::Base
scope :useful_scope, lambda { |value1, value2|
where(:value1 => value1, :value2 => value2)
end
end
Class B < A
set_table_name "b"
end
The problem I'm encountering is that the table name in the sql queries still reference Class A's Table:
A.useful_scope("alpha", "beta").to_sql
=> "SELECT \"a\".* FROM \"a\" WHERE \"a\".\"value1\" = 'alpha' AND \"a\".\"value2\" = 'beta'"
B.useful_scope("alpha", "beta").to_sql
=> "SELECT \"b\".* FROM \"b\" WHERE \"a\".\"value1\" = 'alpha' AND \"a\".\"value2\" = 'beta'"
Note that the table names in the WHERE statement still refer to A. I am modifying an existing gem with various dependencies on the Class A scope throughout, so I need to maintain it's current syntax. I want to maintain the table name specifiers in the WHERE clause SQL to ensure that the scope will behave well when nested with other named scope definitions.
I have tried the following:
- Using a lambda parameter for the table name. This broke the syntax for other references to the scope which only provided the current 2 properties.
- Using an abstract class to define the scope. The same binding of the table name occurred, but using the class name of the Abstract Class.
- Using a scope defined in a module and including the module. The same binding of the table name occurred.
Is there a way that I can force the scope to be evaluated on each inherited class, so that it isn't explicitly mapped to the parent classes table?