I want to select the Resources that are used by the most users.
The Models:
class Resource < ActiveRecord::Base
has_many :users, :through => :kits
has_many :kits
class User < ActiveRecord::Base
has_many :resources, :through => :kits
has_many :kits, :dependent => :destroy
class Kit < ActiveRecord::Base
belongs_to :resource
belongs_to :user
I want to create a scope that selects those resources whose resource.users.count > 3
How can I do this in Rails?
Thanks
I am getting close but still have some issues:
scope :most, group('resources.id, resources.title, resources.url, resources.description, resources.author, resources.price, resources.created_at, resources.updated_at').joins(:users).having("count(user_id) > ?",5)
I have had to include all the fields for a resource because Postgresql is giving an error as follows:
ActiveRecord::StatementInvalid: PG::Error: ERROR: column "resources.category_id" must appear in the GROUP BY clause or be used in an aggregate function
There must be a way to include all the fields without having to type out each one
This should work: