简体   繁体   English

正确关联 Ruby on Rails

[英]Correct Associations Ruby on Rails

Can someone help me to correct my associations?有人可以帮我纠正我的联想吗?

I have the following models:我有以下型号:

User, Developer, Application, Comments, Rating, Permission

Requirements:要求:

A user can be a Developer or not.
A user can have Default Permissions and Permissions for each application
A user can install multiple Applications
A user can comment and rate multiple Applications
A developer can develop multiple applications
An application can request a list of permissions. 

I already created some associations but I believe its not 100% correct or an easier way to do it exist.我已经创建了一些关联,但我相信它不是 100% 正确或存在更简单的方法。

Can someone suggest me a correct way to do it?有人可以建议我正确的方法吗?

You are confusing models with authorization.您将模型与授权混淆了。

You should check out CanCan for role based authorization.您应该查看 CanCan 以获得基于角色的授权。 For example you don't need your developer model since its just a user with a different role/permissions.例如,您不需要开发人员 model,因为它只是具有不同角色/权限的用户。

Edit: Changed 'role based authentication' to 'role based authorization'.编辑:将“基于角色的身份验证”更改为“基于角色的授权”。 As the comment below points out the difference between authentication and authorization.正如下面的评论指出了身份验证和授权之间的区别。

I think this is what you want as far as your model work.我认为这就是您想要的 model 工作。 You can use a join model to manage your application permissions, and use Rails STI to manage what each type of user can do, whether it's developing or not.您可以使用连接 model 来管理您的应用程序权限,并使用 Rails STI 来管理每种类型的用户可以做什么,无论是否正在开发。

user.rb用户.rb

class User < ActiveRecord::Base
  has_many :apps
  has_many :comments, :through => :user_comments
  has_many :ratings, :through => :user_ratings
end

comment.rb评论.rb

class Comment < ActiveRecord::Base
  belongs_to :user
end

rating.rb评级.rb

class Rating < ActiveRecord::Base
  belongs_to :user
end

user_comment.rb用户评论.rb

class UserComment < ActiveRecord::Base
  belongs_to :app
end

user_rating.rb user_rating.rb

class UserRating < ActiveRecord::Base
  belongs_to :app
end

normal_user.rb (STI) normal_user.rb (STI)

class NormalUser < User
end

developer.rb (STI)开发人员.rb (STI)

class Developer < User
end

app.rb应用程序.rb

class App < ActiveRecord::Base
  has_many :permissions, :through => :app_permissions
  has_many :user_comments
  has_many :user_ratings
end

permission.rb权限.rb

class Permission < ActiveRecord::Base
  belongs_to :app
end

app_permission.rb app_permission.rb

class AppPermission < ActiveRecord::Base
end

I agree with @Mark, don't use STI.我同意@Mark,不要使用 STI。 The better way will be implement as suggested by @Chris Barretto except for STI and use CanCan for role based authentication.更好的方法将按照@Chris Barretto 的建议实施,STI 除外,并使用CanCan进行基于角色的身份验证。 The change for User model will be:用户 model 的更改将是:

class User < ActiveRecord::Base
    has_many :apps
    has_many :comments, :through => :user_comments
    has_many :ratings, :through => :user_ratings
    has_many :roles
end

And there will be another model for Role:并且将有另一个 model 用于角色:

class Role  < ActiveRecord::Base
    has_many :users
end

If you are using gems like Devise for authentication, it will be much easy.如果您使用Devise之类的 gem 进行身份验证,那将非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM