我工作的发动机自动创建关联,其中任何模型都可以有允许的has_many协会的允许:通过许多现有关联
class Permit < ActiveRecord::Base
belongs_to :permissible, polymorphic: true
end
module Permissible
def self.included(base)
base.class_eval do
has_many :permits, as: :permissible
end
end
class Group < ActiveRecord::Base
include Permissible
end
class GroupAllocation < ActiveRecord::Base
belongs_to :person
belongs_to :group
end
class Person < ActiveRecord::Base
include Permissible
has_many :group_allocations
has_many :groups, through: :group_allocations
end
class User < ActiveRecord::Base
belongs_to :person
end
因此,集团的has_many:许可证和人的has_many:许可证。我想要做的是在用户上动态创建关联,使用许可证关联作为源,并通过相同的方式将其他模型上的关联关联到用户。这可以手动完成(在轨3.1+)有:
class Person
has_many :group_permits, through: :person, source: :permits
end
class User
has_many :person_permits, through: :person, source: :permits, class_name: Permit
has_many :person_group_permits, through: :person, source: :group_permits, class_name: Permit
end
然而,在实践中,允许将包括在许多车型,所以我想写上的用户(在另一个模块实际上是一个类的方法,但不需要混淆更多的东西),它可以遍历User.reflect_on_all_associations并创建一个新的关联数组,这可能是每个关联都很深的关联。
寻找关于如何在rails 3.2.8中干净利落的输入。