复杂的 has_many 关系和 Rails
我有一些帐户和用户,目前是脱节的。
我需要用户能够成为管理员、编辑或任何(和许多)帐户。
目前,我有这个:
account.rb
has_many :memberships, :dependent => :destroy
has_many :administrators, :through => :memberships, :source => :user, :conditions => {'memberships.is_admin' => true}
has_many :editors, :through => :memberships, :source => :user, :conditions => {'memberships.is_editor' => true}
user.rb
has_many :memberships
has_many :accounts, :through => :memberships
has_many :editor_accounts, :through => :memberships, :source => :account, :conditions => {'memberships.is_editor' => true}
has_many :administrator_accounts, :through => :memberships, :source => :account, :conditions => {'memberships.is_admin' => true}
本质上,我想要实现的是一种很好的简单的建模方法,它以一种很好的简单方式工作。例如,能够执行以下操作将非常有用:
@account.administrators << current_user
current_user.adminstrator_accounts = [..]
等
I have some accounts, and users, which are disjointed at the moment.
I need users to be able to be admins, or editors, or any (and many) accounts.
At the moment, I have this:
account.rb
has_many :memberships, :dependent => :destroy
has_many :administrators, :through => :memberships, :source => :user, :conditions => {'memberships.is_admin' => true}
has_many :editors, :through => :memberships, :source => :user, :conditions => {'memberships.is_editor' => true}
user.rb
has_many :memberships
has_many :accounts, :through => :memberships
has_many :editor_accounts, :through => :memberships, :source => :account, :conditions => {'memberships.is_editor' => true}
has_many :administrator_accounts, :through => :memberships, :source => :account, :conditions => {'memberships.is_admin' => true}
Essentially, what I am trying to acheive is a nice simple way of modelling this that works in a nice simple way. For instance, being able to do the following this would be really useful:
@account.administrators << current_user
current_user.adminstrator_accounts = [..]
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够执行此操作,但您使用的符号可能会干扰自动作用域应用程序:
当且仅当条件键与关联上的列名称匹配时才应应用条件。只要
users
表没有is_admin
列,就可以了。请注意,对于这样的事情使用多个布尔标志可能会很尴尬。可以同时担任管理员和编辑吗?您可能会更好地使用一个简单的角色列,然后使用它:
从索引角度来看,多用途列通常比多个单一用途列更好。您必须为每个
is_admin
类型列建立索引,并且通常需要对多个键执行此操作。这很快就会变得混乱。You should be able to do this, but it might be the notation you've used that interferes with the auto scope application:
The conditions should be applied if and only if the condition keys match the column names on the association. So long as the
users
table doesn't have ais_admin
column, this will be fine.As a note, having multiple boolean flags for something like this can be awkward. Is it possible to be an admin and an editor? You may be better off with a simple role column and then use that:
A multi-purpose column is often better than a multitude of single-purpose columns from an indexing perspective. You will have to index each and every one of these
is_admin
type columns, and often you will need to do it for several keys. This can get messy in a hurry.