Skip to content

Instantly share code, notes, and snippets.

@thedelchop
Created December 14, 2010 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedelchop/739889 to your computer and use it in GitHub Desktop.
Save thedelchop/739889 to your computer and use it in GitHub Desktop.
class ActiveMembership < ActiveRecord::Base
belongs_to :member
has_many :active_membership_motions
has_one :qualifying_motion, :through => :active_membership_motions,
:source => :motion,
:conditions => {:active_membership_motions => {:qualifying => true}}
has_one :disqualifying_motion, :through => :active_membership_motions,
:source => :motion,
:conditions => {:active_membership_motions => {:qualifying => false}}
validates :qualifying_motion, :presence => true
# Finds all active memberships for a given time
# @param [Date, Time, DateTime] time The Date and Time in which to search for active memberships
# @return [Array] The list of active memberships, which were active at a given time
def self.active_at(time)
where("started_at <= ?", time).
where("ended_at >= ? OR ended_at IS NULL", time)
end
# Finds all members active at a given date or date+time
# @param [Date, Time, DateTime] time The Date and Time in which to search for active members
# @return [Array] A unique list of members, active at the given time
def self.members_active_at(time)
members = active_at(time).map(&:member)
members.uniq
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment