Rails 4 has many end-to-end and volume orders

I have this model:

class User < ActiveRecord::Base has_many :customers, -> { order('customers.name ASC') } has_many :stores, -> { order('company_stores.id ASC').uniq }, through: :customers end 

When i try

 user.stores 

I have this error:

 PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list 

because Rails does SELECT DISTINCT of company_stores.* , but customers.name also appears in ORDER BY

Should I refuse an order in associations?

+7
ruby-on-rails
source share
1 answer

As the error message shows, PG requires that the select expression be included in the selection, so select('stores.*, company_stores.id').order('company_stores.id ASC').uniq or the like should do the trick.

+7
source share

All Articles