I am new to Ruby on Rails. I have a problem- how to get only latest records from has many relationship in model. My code is like this:
class User < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :user
scope :rating, -> { where(rating: 5) }
end
What I would like to achieve is: I'd like to get all latest (last) book for each user and than filter this books using scope rating. I can do this using for each loop (Looping through User.books and get book.last)- but this is very inefficient because I have a lot of data. Do you have any ideas how I can do this? maybe some kind of scope chaining with each other?