множественные отношения принадлежат к трем моделям - PullRequest
1 голос
/ 12 апреля 2011

Ситуация такая ...

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

ВОПРОС: как мне заполнить все три внешних ключа в таблице ролей-членов ... когда я выполняю org.users.push (u), это создает запись, но роль role_id опущена ...

1 Ответ

1 голос
/ 12 апреля 2011

В этом случае я, вероятно, создам сам объект RoleMembership, например:

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