Reputation: 32190
I have a User
model and a :friends
association
When I create the data, I take the user and its first 5 friends
I could do
User.includes(:friends)
but that will take all friends.
Is there a way to take only the first 5 friends?
Upvotes: 2
Views: 668
Reputation: 7744
I suppose the following could work:
class User < ActiveRecord::Base
has_many :friends
has_many :top_friends, -> { limit(5).order(:id) }, class_name: Friend
end
User.includes(:top_friends)
Upvotes: 2