Well, more responsibility is put into the User model than necessary, and there is no good reason for this.
We can first define the area in the EventUser model, because where it really belongs, for example:
class EventUser < ActiveRecord::Base belongs_to :event belongs_to :user scope :active, -> { where(active: true) } scope :inactive, -> { where(active: false) } end
Now the user can have both types of events: active events, as well as inactive events, so we can define the relationship in the User model as follows:
class User < ActiveRecord::Base has_many :active_event_users, -> { active }, class_name: "EventUser" has_many :inactive_event_users, -> { inactive }, class_name: "EventUser" has_many :inactive_events, through: :inactive_event_user, class_name: "Event", source: :event has_many :active_events, through: :active_event_users, class_name: "Event", source: :event end
The beauty of this method is that the functionality of an active or inactive event belongs to the EventUser model, and if in the future the functionality needs to be changed, it will be changed only in one place: the EventUser model, and the changes will be reflected in all other models.
Arslan Ali Nov 10 '16 at 10:06 2016-11-10 10:06
source share