I have the following models
class User
attr_accesible :first_name, :phone_number
has_one :user_extension
end
class UserExtension
attr_accessible :comp
@users = User.order("user_extension.company desc")
should work fine.
Old topic, and perhaps off topic, but I needed this. FWIW:
User.includes(:user_extension).order('user_extensions.company ASC')
Lets make it more interesting, create more associations.
User.includes(user_extension: :company).order('companies.name ASC')
User.includes(user_extension: { company: :guilds }).order('guilds.secret_knock ASC')
Try this:
@users = User.includes(:user_extension).order("user_extensions.company desc")
I think you need at order
: user_extensions
, not user_extension
Merge can make the query smaller/saner-looking, and it benchmarked faster for me in Rails 4.x:
@users = User.joins(:user_extension).merge(UserExtension.order(company: :desc))