Note that there are some explanatory texts on larger screens.

plurals
  1. PONamed scopes with inheritance in Rails 3 mapping to wrong table
    text
    copied!<p>I am attempting to use inheritance from a class which has a named scope:</p> <pre><code>Class A &lt; ActiveRecord::Base scope :useful_scope, lambda { |value1, value2| where(:value1 =&gt; value1, :value2 =&gt; value2) end end Class B &lt; A set_table_name "b" end </code></pre> <p>The problem I'm encountering is that the table name in the sql queries still reference Class A's Table:</p> <pre><code>A.useful_scope("alpha", "beta").to_sql =&gt; "SELECT \"a\".* FROM \"a\" WHERE \"a\".\"value1\" = 'alpha' AND \"a\".\"value2\" = 'beta'" B.useful_scope("alpha", "beta").to_sql =&gt; "SELECT \"b\".* FROM \"b\" WHERE \"a\".\"value1\" = 'alpha' AND \"a\".\"value2\" = 'beta'" </code></pre> <p>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.</p> <p>I have tried the following:</p> <ul> <li>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.</li> <li>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.</li> <li>Using a scope defined in a module and including the module. The same binding of the table name occurred.</li> </ul> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload