Take care when joining and selecting on scopes

Updated . Posted . Visible to the public.

Occasionally some complex query must be processed on the database because building thousands of Ruby objects is impracticable.

Many times you would use scope options, like this:

users = User.scoped(
  :joins => 'INNER JOIN orders joined_orders ON users.id = joined_orders.user_id',
  :conditions => [ 'joined_orders.date BETWEEN ? AND ?', start_date, end_date ],
  :select => '*, SUM(joined_orders.amount) AS amount_sum',
  :group => 'users.id'
)

You get ActiveRecord objects and you can ask each of them about its amount_sum -- which is great, but your User objects may not have the actual ID from their row in the users table but one of the joined table.

Ensure that you get the correct user IDs by selecting only from the users table:
users = User.scoped(
:joins => 'INNER JOIN orders joined_orders ON users.id = joined_orders.user_id',
:conditions => [ 'joined_orders.date BETWEEN ? AND ?', start_date, end_date ],
:select => 'users., SUM(joined_orders.amount) AS amount_sum', # note the "users." instead of "*" above
:group => 'users.id'
)

Arne Hartherz
Last edit
Keywords
mysql, primary, key
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2010-11-25 10:40)