Rails: ActiveRecord query regarding size of association -
i'm trying figure out how produce query, using activerecord.
i have following models
class activity < activerecord::base attr_accessible :limit, ... has_many :employees end class user < activerecord::base belongs_to :activity end
each activity has limit, say, integer attribute containing maximum amount of users may belong it.
i'm looking way select activities have spots available, i.e. number of users smaller limit.
any ideas?
thanks
i think sql syntax aim be:
select * activities activities.limit > ( select count(*) users users.activity_id = activities.id)
in rails-speak ...
activity.where("activities.limit > (select count(*) users users.activity_id = activities.id)")
not sure whether column name "limit" going give problems it's reserved word. might have quote in sql.
i'd consider counter cache users on activities table, make perform better. databases support partial index rows users counter cache < limit.
Comments
Post a Comment