Get child model with rails created_at desc scope

To change the acquisition method of child model data with desc or asc, you can use the following method.

scope :order_asc, -> { includes(:messages).order('messages.created_at ASC') }

Reference article

Ruby On Rails: Order by child attribute

Recommended Posts

Get child model with rails created_at desc scope
How to get along with Rails
[Ruby on Rails] Model test with RSpec
Rails beginners tried to get started with RSpec
Create an EC site with Rails5 ⑤ ~ Customer model ~
Refactoring with scope
[Rails] Model Association (Association)
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
[My memo] Let's get along with Pry / DB with Rails
How to get started with creating a Rails app