I’m working an events feature, and I’m running into a rather involved query.
Here’s the models for reference:
class Event < ApplicationRecord
has_one :party
belongs_to :host, foreign_key: :host_id, class_name: 'User'
belongs_to :convention, optional: true
has_many :join_requests
...
end
I’m looking to check which Events are accepting members, by checking the member_limit
of the associated Party.
class Party < ApplicationRecord
belongs_to :event
belongs_to :host, foreign_key: :host_id, class_name: 'User'
has_many :user_parties
has_many :members, through: :user_parties, source: 'user'
...
end
Party
has an integer column: member_limit
— which only allows a set amount of party members.
So far I have a scope on Event that looks like this:
scope :accepting_members, -> () do
joins(party: :user_parties).group('parties.member_limit').select('count(user_parties.user_id) < parties.member_limit')
end
Which doesn’t work too well.
Am I on the right track?
I tried using the COUNT() aggregate, but it only returns a hash of integers.
2
Answers
I think you don’t need any complex joins you can do it like this.
Since relation is as follows
You can do it like this.
This will return boolean. if member limit is not reached it will return true otherwise false.
This is going to be a bit more complicated then you initially thought if you want usable output and there are quite a few ways to do this.
One way is to create a subquery for the event ids:
Writing this in ActiveRecord/Arel gets a bit hairy but bear with me:
Other ways to do this would be to use
EXIST
or a lateral join.