ruby on rails - Ordering by a column in an associated model -
i trying make activerecord query order results value of 1 of columns in associated model:
i have chats model has one many
relationship messages model.
chats has_many messages
, message belongs_to chat
and controller, want list of chats, ordered created_at
of associated message.first, eg:
@chats = current_user.chats.includes(:messages).order("messages.first.created_at").paginate(page: params[:page])
or that.
my question how can achieve kind of ordering associated model relationship? contributions appreciated.
also add .references(:messages)
this pull messages in join rather separate query.
Comments
Post a Comment