你可以做这样的事情。
class Email < ActiveRecord::Base
has_many :email_users
has_many :users, :through => :email_users
scope :approved, where(approved: true )
scope :publishers, joins(:email_users => :user).where(:users => {:role => "publisher"})
scope :subscribers, joins(:email_users => :user).where(:users => {:role => "subscriber"})
end
class EmailUser < ActiveRecord::Base
belongs_to :email
belongs_to :user
end
class User < ActiveRecord::Base
has_many :email_users
has_many :emails, :through => :email_users
scope :publisher, where(:role => "publisher" )
scope :subscriber, where(:role => "subscriber" )
end
然后,您可以像这样简单地从发布者那里获得批准的电子邮件。
Email.publishers.approved
并从订阅者那里获得电子邮件。
Email.subscribers
并从出版商那里获得电子邮件。
Email.publishers