I got polymorphic has many through association
# a/m/documnet.rb
class Document < ActiveRecord::Base
has_many :possessions, as: :belonging
has_many :clients, :through => :possessions, :source => :owner, :source_type => 'Client'
end
in console
Document.new.clients
#=> []
in views with simple form
= f.association :clients, as: :check_boxes, collection: Client.all
it works without problem.
—
Issue starts when I want to dynamically associate has many resources
class Document < ActiveRecord::Base
has_many :possessions, as: :belonging
def self.possession_owner_classes
[Client, SomethingElse]
end
possession_owner_classes.each do |possession_class|
has_many possession_class.model_name.underscore.pluralize, :through => :possessions, :source => :owner, :source_type => possession_class.model_name
end
end
in console
Document.new.clients
#=> []
in views with simple form
= f.association :clients, as: :check_boxes, collection: Client.all
will throw
Association :clients not found
so Rails know about this association but simple form doesn’t :-/ any ideas please ?
ok I found the error,
should be
so association name must be symbol not string
huh :-\