Church IO

09 Mar 2016 . launch . Comments #ChMS

Church IO is a community who feels church software is expensive. These developers write open source software for churches. One Body is a members portal where church members get together and socialize.

Andrew Gauger is a recent addition to the team. He writes features that have been sitting in the issues list, waiting patiently. The most recent update to OneBody is for messages to be sent to a group subset.

To facilitate this, the existing Message model gained a new association: has and belongs to many members. The main challenge was the current Message model had two duties, sending group mail and private mail (one member, not necessarily in a group).

  has_and_belongs_to_many :members,
                          class_name: 'Person',
                          join_table: :members_messages,
                          association_foreign_key: member_id

With the assicaition in place, Rails knows to query the join table members_messages which will have a column for member_id and message_id. If there are 3 records where the message_id equals 1, then message 1 has three members.

members_messages

message_id member_id
1 5
1 7
1 10

To create the join table, the rails scaffold generator can be invoked with
rails generate migration CreateMessagesMembersJoinTable messages members

Since the functionality of the Message needs to handle dispatch to the entire group by default, the behavior only changes when at least one member exists. To determine if the Message is destined for a particular member, two scopes were created:

    scope(:for_user, lambda do |user|
      joins(:members).where('members_messages.member_id = ? OR messages.person_id = ?', user.id, user.id).uniq
    end)
    scope :for_whole_group, -> { includes(:members).where(members_messages: { :message_id => nil }) }

Join the discussion at Github