Many to many relationship , rails -
i have many-to-many relation between users , channels subscribe to. when @ model dependency between user , user channels or channels , users channel instead there direct connection between users , channels. how put users channels between two? user model
class user < activerecord::base acts_as_authentic roles = %w[admin moderator subscriber] has_and_belongs_to_many :channels has_many :channel_mods named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**roles.index(role.to_s)} > 0 "} } def roles roles.reject { |r| ((roles_mask || 0) & 2**roles.index(r)).zero? } end def roles=(roles) self.roles_mask = (roles & roles).map { |r| 2**roles.index(r) }.sum end def role_symbols role.map |role| role.name.underscore.to_sym end end end channel model
class channel < activerecord::base acts_as_taggable acts_as_taggable_on :tags has_many :messages has_many :channel_mods has_and_belongs_to_many :users end userschannel model
class userschannels < activerecord::base end
see has_many :through documentation on rails guides guides through configuring has_many relationship intervening model.
Comments
Post a Comment