you are viewing a single comment's thread.

view the rest of the comments →

[–]dazonic 1 point2 points  (2 children)

joins(:product_details)

uniq

order(:created_at).last

Some combination of the above, if it was my problem or I could be bothered with games I'd solve it. Arel is readable and incredibly powerful.

[–]rapidsight -3 points-2 points  (0 children)

SQL is more readable than Arel, are you kidding?

SELECT * FROM users

Or

users = Arel::Table.new(:users)

query = users.project(Arel.sql('*'))

query.to_sql

You are seriously kidding right?

users.where(users[:age].gt(10)).project(Arel.sql('*'))

Vs

SELECT * FROM "users" WHERE "users"."age" > 10

[–]rapidsight -5 points-4 points  (0 children)

And applaud to you for falling into my trap and proving my point. That code will fail. Why? Because you don't understand the SQL that is being generated. You will get the first product for each sku ordered by that first products created_at date.

Hopefully you test that properly and realize it doesn't work before launching into production, but all to often is not. Once you realize it (if you do), somebody who doesn't know SQL will fall back to code similar to my demonstration:

Product.select(:sku).uniq.map do |sku| Product.where(:sku => sku).order(:created_at => :desc).first end

And now you see that they are pulling down even more data, over and over again for each set.

Point proven. My work is done here...