Read more

Take care when joining and selecting on scopes

Arne Hartherz
November 25, 2010Software engineer at makandra GmbH

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

Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

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'
)

Posted by Arne Hartherz to makandra dev (2010-11-25 11:40)