Rollify Gem用户has_many民意调查/角色和民意调查有很多答案,只有一个管理员

发布于 2025-01-23 18:31:20 字数 1098 浏览 3 评论 0原文

尝试使用用户has_many的民意调查/角色创建一个民意调查应用程序,但民意调查有很多答复器,只有一个管理员。

user.rb

class User < ApplicationRecord
  rolify
  has_many :polls, dependent: :destroy, through: :roles, source: :resource, source_type: :Poll
end

poll.rb

class Poll < ApplicationRecord
  resourcify

  # has_many :users, through: :roles, class_name: 'User', source: :users
  has_many :answerers, -> { where(:roles => {name: ::answerers}) }, through: :roles, class_name: 'User', source: :users
  belongs_to :admin, -> { where(:roles => {name: :admin}) }, through: :roles, class_name: 'User', source: :users
end

不断遇到以下错误:

Unknown key: :through. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :required, :touch, :polymorphic, :counter_cache, :optional, :default

错误是由Poll.RB中的此行引起的。

belongs_to :admin, -> { where(:roles => {name: :admin}) }, through: :roles, class_name: 'User', source: :users

Trying to creating a poll app with a User has_many polls/roles but the Poll has many answerers and only one admin.

user.rb

class User < ApplicationRecord
  rolify
  has_many :polls, dependent: :destroy, through: :roles, source: :resource, source_type: :Poll
end

poll.rb

class Poll < ApplicationRecord
  resourcify

  # has_many :users, through: :roles, class_name: 'User', source: :users
  has_many :answerers, -> { where(:roles => {name: ::answerers}) }, through: :roles, class_name: 'User', source: :users
  belongs_to :admin, -> { where(:roles => {name: :admin}) }, through: :roles, class_name: 'User', source: :users
end

keep running into the following error:

Unknown key: :through. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :required, :touch, :polymorphic, :counter_cache, :optional, :default

The error is caused by this line in poll.rb:

belongs_to :admin, -> { where(:roles => {name: :admin}) }, through: :roles, class_name: 'User', source: :users

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

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

发布评论

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

评论(1

江城子 2025-01-30 18:31:20

您已经遇到了一个经典的错觉,这是由于属于 vs has_one的混淆而引起的。

属于将外键列放在 this 模型表上。当您使用anders_to:Admin Rails假设您有polls.admin_id列,指向gyd> gysins.id

属于关联永远不会间接,因此不要通过:选项具有HAS_ONE做。

如果您想保证一项民意调查只能有一个您不想在此特定情况下使用Rolify的管理员,而是使用:

class Poll < ApplicationRecord
  resourcify
  # ...
  belongs_to :admin, class_name: 'User'
end

这是完全可以的。虽然Rolify提供了一种方便的方法来添加角色,而不是您应用程序中的每个关联都应将其插入其中。直接链接的效率比浏览两个联盟表的效率更高,并提供了一个只有一个值的guarentee。

虽然您可能会在想:“如果我只是使用has_one,该怎么办?”。 HAS_ONE没有给出只有民意调查的admin的guarentee,它只是将限制1添加到查询中。

rolify使用users_roles habtm加入表来加入用户和角色,例如,您无法在该表中添加唯一性约束而不会影响整个系统。

You have run into a classic missconception caused by confusing semantics of belongs_to vs has_one.

A belongs_to places the foreign key column on this models table. When you use belongs_to :admin Rails assumes that the you have polls.admin_id column that points to admins.id.

belongs_to assocations are never indirect and thus do not a have a through: option. has_one does.

If you want to guarentee that a Poll can only ever have one admin you want to not use Rolify for this specific case and instead use:

class Poll < ApplicationRecord
  resourcify
  # ...
  belongs_to :admin, class_name: 'User'
end

That's fully OK. While Rolify provides a convenient way to add roles not every association in your application should be shoehorned into it. A direct link is way more efficient then going through two join tables and provides a guarentee that there is only one value.

While you might be thinking "Well what if I just use has_one instead?". has_one gives no guarentee that that a Poll only has admin - it just adds LIMIT 1 to the query.

Rolify uses the users_roles HABTM join table to join users and roles and there is no way you can for example add a uniqueness constraint to that table without it impacting the entire system.

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