Rollify Gem用户has_many民意调查/角色和民意调查有很多答案,只有一个管理员
尝试使用用户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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经遇到了一个经典的错觉,这是由于
属于
vshas_one
的混淆而引起的。属于
将外键列放在 this 模型表上。当您使用anders_to:Admin
Rails假设您有polls.admin_id
列,指向gyd> gysins.id
。属于
关联永远不会间接,因此不要通过:选项具有。
HAS_ONE
做。如果您想保证一项民意调查只能有一个您不想在此特定情况下使用Rolify的管理员,而是使用:
这是完全可以的。虽然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
vshas_one
.A
belongs_to
places the foreign key column on this models table. When you usebelongs_to :admin
Rails assumes that the you havepolls.admin_id
column that points toadmins.id
.belongs_to
assocations are never indirect and thus do not a have athrough:
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:
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 addsLIMIT 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.