Order on join model

Hi

I have three models, User, Like and Photo.

A User can like many Photos and a Photo can have many Likers.

In my User model I have:

has_many :likes, foreign_key: “liker_id”, dependent: :destroy

has_many :liked_photos, through: :likes

Like model:

belongs_to :liker, class_name: “User”

belongs_to :liked_photo, class_name: “Photo”, counter_cache: true

Now I can do something like @user.liked_photos to get the photos that the user likes. But I want to order this based on when the Like was created.

How can I order by my Like model?

If I do this in User:

has_many :likes, -> { order(“created_at DESC”) }, foreign_key: “liker_id”, dependent: :destroy

It orders by the photos created_at when I call @user.latest_photos

Any ideas?

Ok, this seem to work fine
@user.liked_photos.order(“likes.created_at DESC”)