Audited:与HABTM关系关联的审计
Audited: Associated audits with HABTM relation
我有模型 user
和 role
class User < ApplicationRecord
rolify strict: true
has_many :roles, through: :users_roles
has_associated_audits
class Role < ApplicationRecord
has_and_belongs_to_many :users, join_table: :users_roles
audited associated_with: :users, join_table: :users_roles
创建新角色时出现错误:
2.4.4 :373 > User.first.add_role Role.pi, ProjectRequest.find(319)
User Load (0.7ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
ProjectRequest Load (0.6ms) SELECT `project_requests`.* FROM `project_requests` WHERE `project_requests`.`id` = 319 LIMIT 1
Role Load (0.6ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`name` = 'pi' AND `roles`.`resource_type` = 'ProjectRequest' AND `roles`.`resource_id` = 319 ORDER BY `roles`.`id` ASC LIMIT 1
(0.2ms) BEGIN
SQL (0.6ms) INSERT INTO `roles` (`name`, `resource_type`, `resource_id`, `created_at`, `updated_at`) VALUES ('pi', 'ProjectRequest', 319, '2018-06-19 11:40:13', '2018-06-19 11:40:13')
(54.3ms) ROLLBACK
NoMethodError: undefined method `primary_key' for User::ActiveRecord_Associations_CollectknowProxy:Class
我现在真的不知道是什么问题,我是不是指定错了什么?
如果是 has_and_belongs_to_many :users 并且 join_table 在角色 table 中是 users_roles,那么看起来关联有问题,它也应该是 has_and_belongs_to_many :roles, join_table: :users_roles,这将建立 HABM 关系。
我已经通过本教程解决了这个问题:
http://blog.flatironschool.com/why-you-dont-need-has-and-belongs-to-many/
我所做的是删除 HABTM 关系并为连接创建模型 table。
class User < ApplicationRecord
rolify strict: true
has_many :users_roles
has_many :roles, through: :users_roles, dependent: :destroy
has_associated_audits
class Role < ApplicationRecord
has_many :users_roles
has_many :users, through: :users_roles, dependent: :destroy
audited associated_with: :users
class UsersRole < ApplicationRecord
# audited associated_with: :role
audited associated_with: :user
belongs_to :user
belongs_to :role
end
现在,当在 UsersRole
实例中更改创建审计时。
问题是,更改仅包含 ids
个被破坏的列,因此您无法确定它是什么。
这个问题在那里得到了部分解决:https://github.com/collectiveidea/audited/issues/72#issuecomment-398756380
我有模型 user
和 role
class User < ApplicationRecord
rolify strict: true
has_many :roles, through: :users_roles
has_associated_audits
class Role < ApplicationRecord
has_and_belongs_to_many :users, join_table: :users_roles
audited associated_with: :users, join_table: :users_roles
创建新角色时出现错误:
2.4.4 :373 > User.first.add_role Role.pi, ProjectRequest.find(319)
User Load (0.7ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
ProjectRequest Load (0.6ms) SELECT `project_requests`.* FROM `project_requests` WHERE `project_requests`.`id` = 319 LIMIT 1
Role Load (0.6ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`name` = 'pi' AND `roles`.`resource_type` = 'ProjectRequest' AND `roles`.`resource_id` = 319 ORDER BY `roles`.`id` ASC LIMIT 1
(0.2ms) BEGIN
SQL (0.6ms) INSERT INTO `roles` (`name`, `resource_type`, `resource_id`, `created_at`, `updated_at`) VALUES ('pi', 'ProjectRequest', 319, '2018-06-19 11:40:13', '2018-06-19 11:40:13')
(54.3ms) ROLLBACK
NoMethodError: undefined method `primary_key' for User::ActiveRecord_Associations_CollectknowProxy:Class
我现在真的不知道是什么问题,我是不是指定错了什么?
如果是 has_and_belongs_to_many :users 并且 join_table 在角色 table 中是 users_roles,那么看起来关联有问题,它也应该是 has_and_belongs_to_many :roles, join_table: :users_roles,这将建立 HABM 关系。
我已经通过本教程解决了这个问题: http://blog.flatironschool.com/why-you-dont-need-has-and-belongs-to-many/
我所做的是删除 HABTM 关系并为连接创建模型 table。
class User < ApplicationRecord
rolify strict: true
has_many :users_roles
has_many :roles, through: :users_roles, dependent: :destroy
has_associated_audits
class Role < ApplicationRecord
has_many :users_roles
has_many :users, through: :users_roles, dependent: :destroy
audited associated_with: :users
class UsersRole < ApplicationRecord
# audited associated_with: :role
audited associated_with: :user
belongs_to :user
belongs_to :role
end
现在,当在 UsersRole
实例中更改创建审计时。
问题是,更改仅包含 ids
个被破坏的列,因此您无法确定它是什么。
这个问题在那里得到了部分解决:https://github.com/collectiveidea/audited/issues/72#issuecomment-398756380