我工作的發動機自動創建關聯,其中任何模型都可以有允許的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中乾淨利落的輸入。