Has_one or Has_many?
I have four models.
Application, Permission, User and AppPermissions
My AppPermission looks like:
AppPermission Application Permission User
1 1 1 1
2 1 2 2
3 1 3 3
4 1 4 4
So an Application can have different permission from different Users.
Model: AppPermission:
belongs_to :application
belongs_to :permission
belongs开发者_开发百科_to :user
What about Application , Permission and User ? Where to use has_many through or has_one through?
Application:
has_many :users
has_many :permissions
For users, you need more information, do you have more than one application? if so then:
User:
has_many :applications
has_many :appPermissions
Permission:
has_many :appPermissions
belongs_to :application
I don't see use case for having has_many :through
with a three-way association. What a point having all permissions for a user without mentioning what application they are for? Most probably you will need queries like:
user.app_permissions.find_all_by_application_id(application).map(&:permission)
or
user.app_permissions.find_by_application_id_and_permission_id(application, permission)
All you need for these queries:
User:
has_many :app_permissions
AppPermission:
belongs_to :permission
精彩评论