Ruby on Rails Friday, August 9, 2013

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



Den fredagen den 9:e augusti 2013 kl. 10:55:39 UTC+2 skrev Linus Pettersson:
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?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/70394961-bd41-47b1-a987-67476db6d0c2%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment