Help me please. I have some model which has_many association with other model. For example: profile => has_many :statistics And inside of statistic model I have some scope:
scope last_ten, limit(10).order('online desc')
And question is how can I use eager load for this scope? I don't need every record of statistics for profile. Only scoped.
Now I can use only
User.profiles.includes(:statistics)
Thanks.
As explained here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
It's better to define a custom relation:
class Profile < ActiveRecord::Base
has_many :most_recent_stats, :class_name => 'Statistic', :order => 'online DESC', :limit => 10
...
end
User.profiles.includes(:most_recent_stats)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With