ruby on rails - multiple belongs_to relationship to three model -


the situation way..

class organization < activerecord::base    has_many :role_memberships   has_many :roles   has_many :users, :through => :role_memberships, :uniq => true  end  class user   has_many :role_memberships   has_many :organizations, :through => :role_memberships, :uniq => true   has_many :roles, :through => :role_memberships, :uniq => true   end  class rolemembership < activerecord::base    belongs_to :organization   belongs_to :role   belongs_to :user  end  class role < activerecord::base   belongs_to :organization   has_many :role_memberships   has_many :users, :through => :role_memberships, :uniq => true end 

the question how populate 3 foreign-keys in rolemembership table..when org.users.push(u) create record role_id left out...

in case create rolemembership object itself, this:

rolemembership.create(:organization_id => org.id, :role_id => role.id, :user_id => user.id) 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -