Ruby on Rails
Friday, August 9, 2013
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/d499eac7-845f-42d5-be96-8ebb153facdf%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment