复杂的 has_many 关系和 Rails

发布于 2024-10-28 14:54:41 字数 1014 浏览 2 评论 0原文

我有一些帐户和用户,目前是脱节的。

我需要用户能够成为管理员、编辑或任何(和许多)帐户。

目前,我有这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-11-04 14:54:41

您应该能够执行此操作,但您使用的符号可能会干扰自动作用域应用程序:

has_many :memberships,
  :dependent => :destroy
has_many :administrators,
  :through => :memberships,
  :source => :user,
  :conditions => { :is_admin => true }

当且仅当条件键与关联上的列名称匹配时才应应用条件。只要 users 表没有 is_admin 列,就可以了。

请注意,对于这样的事情使用多个布尔标志可能会很尴尬。可以同时担任管理员和编辑吗?您可能会更好地使用一个简单的角色列,然后使用它:

has_many :administrators,
  :through => :memberships,
  :source => :user,
  :conditions => { :role => '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:

has_many :memberships,
  :dependent => :destroy
has_many :administrators,
  :through => :memberships,
  :source => :user,
  :conditions => { :is_admin => true }

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 a is_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:

has_many :administrators,
  :through => :memberships,
  :source => :user,
  :conditions => { :role => 'admin' }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文