动态添加/修改activerecord关联

发布于 2024-11-03 00:58:58 字数 860 浏览 0 评论 0原文

我认为 ActiveRecord 不会出现独特的情况,但我似乎找不到任何有类似问题的人,所以这里是:

我有一个用于用户的 User 类和一个定义了功能的 Roles 类用户。例如,您可以有一个角色为“导师”的用户,一个角色为“学生”的用户,以及一个角色为[“导师”,“学生”]的用户:

class User < ActiveRecord::Base
  has_many :roles
end

class Role < ActiveRecord::Base

end

我想要做什么是根据角色添加activerecord关联。显然,学生可能有很多 :courses,导师可能有他们教授的 :subject,学生和导师都可以有很多 :appointments,但将所有这些关联添加到每个 User 实例似乎并不正确要走的路。

子类化 User 似乎也是错误的 - 我想过做 Tutor <用户和学生<用户,并在每个子类中添加适当的关联,但是如果我们有一个学生是导师怎么办?那我们需要一个StudentTutor类吗?如果增加更多的角色,这条路线就显得危险了。

我考虑过做类似的事情:

class User < ActiveRecord::Base

  protected

  after_initialize do
    if self.has_role?(Role::STUDENT)
      has_many :courses # This does not work
    else
      # etc etc etc
    end
  end
end

但我不知道这是否被认为是错误的,或者我什至不知道如何让它发挥作用。处理这种具有关联的用户/角色设置的最佳方法是什么?

I have what I'd think would not be a unique situation with ActiveRecord, but I can't seem to find anyone with a similar issue, so here goes:

I have a User class for users and a Roles class that defines the capabilities of the user. For ex, you could have a user with a role of 'tutor', a user with a role of 'student', and a user with a role of ['tutor', 'student']:

class User < ActiveRecord::Base
  has_many :roles
end

class Role < ActiveRecord::Base

end

What I'd like to do is add activerecord associations based on roles. Clearly, a student may have many :courses, a tutor may have a :subject they teach, and both a student and tutor could have many :appointments, but it doesn't seem like adding all these associations to every User instance is the right way to go.

Subclassing User also seems wrong - I thought about doing Tutor < User and Student < User, and adding the proper associations in each subclass, but what if we have a student that is a tutor? Then we need a StudentTutor class? If more roles are added, this route seems dangerous.

I've considered doing something like:

class User < ActiveRecord::Base

  protected

  after_initialize do
    if self.has_role?(Role::STUDENT)
      has_many :courses # This does not work
    else
      # etc etc etc
    end
  end
end

But I have no idea if this is considered wrong or how I'd even make it work. What's the best method for dealing with this kind of user/role setup with associations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

回忆追雨的时光 2024-11-10 00:58:58

我会为您的问题推荐一个宝石。请参阅相关的SO问题:角色宝石的推荐

I would recommend a gem for your issue. See a related SO question: Recommendation for Role Gem

謌踐踏愛綪 2024-11-10 00:58:58

我会做以下事情:

class User < ActiveRecord::Base
  has_many :roles
end

class Role < ActiveRecord::Base
end

class Tutor < Role
  has_one :subject
end

class Student < Role
  has_many :courses
end

I would do the following:

class User < ActiveRecord::Base
  has_many :roles
end

class Role < ActiveRecord::Base
end

class Tutor < Role
  has_one :subject
end

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