2011-07-26 4 views
0

私は4つのモデルを持っています。だから、アプリケーションが異なるユーザーは異なる権限を持つことができHas_oneまたはHas_many?

AppPermission Application Permission User 
     1    1    1   1 
     2    1    2   2 
     3    1    3   3 
     4    1    4   4 

Application, Permission, User and AppPermissions 

私AppPermissionは次のようになります。

Model: AppPermission: 
belongs_to :application 
belongs_to :permission 
belongs_to :user 

アプリケーション、許可、ユーザーについてはどうなりますか? has_many throughまたはhas_one throughをどこで使うのですか?

+0

あなただけが使用する関連付けを必要とします。どのような関係を実装する必要がありますか? – Anatoly

答えて

0
Application: 
has_many :users 
has_many :permissions 

詳細については、複数のアプリケーションがありますか?そうであれば:

User: 
has_many :applications 
has_many :appPermissions 

Permission: 
has_many :appPermissions 
belongs_to :application 
0

3ウェイ関連のあるhas_many :throughのユースケースはありません。ユーザーがどのアプリケーションを使用しているかを言わずに、すべての権限を持っていることはどうですか?

user.app_permissions.find_all_by_application_id(application).map(&:permission)

または

user.app_permissions.find_by_application_id_and_permission_id(application, permission)

あなたはこれらのクエリのために必要なすべて:おそらく次のようなクエリが必要になります

User: 
has_many :app_permissions 

AppPermission: 
belongs_to :permission 
関連する問題