I have a Bookthat has_many Reviews, and I've added a class method to Book to show 'recently reviewed' books on my home page. I've tried this:
def self.recently_reviewed
Book.joins(:reviews).order('reviews.created_at DESC').limit(5)
end
Which produces many duplicate records, therefore I tried using distinct like so:
Book.joins(:reviews).order('reviews.created_at DESC').distinct.limit(5)
I also tried it before and after the .order, which
ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
I am a bit confused as to how to solve this, should I be dropping down to .select to have some more flexibility?