The association class for a has_many_polymorphs
association.
Push a record onto the association. Triggers a database load for a
uniqueness check only if :skip_duplicates
is
true
. Return value is undefined.
# File lib/has_many_polymorphs/association.rb, line 14 def <<(*records) return if records.empty? if @reflection.options[:skip_duplicates] _logger_debug "Loading instances for polymorphic duplicate push check; use :skip_duplicates => false and perhaps a database constraint to avoid this possible performance issue" load_target end @reflection.klass.transaction do flatten_deeper(records).each do |record| if @owner.new_record? or not record.respond_to?(:new_record?) or record.new_record? raise PolymorphicError, "You can't associate unsaved records." end next if @reflection.options[:skip_duplicates] and @target.include? record @owner.send(@reflection.through_reflection.name).proxy_target << @reflection.klass.create!(construct_join_attributes(record)) @target << record if loaded? end end self end
Clears all records from the association. Returns an empty array.
# File lib/has_many_polymorphs/association.rb, line 70 def clear(klass = nil) load_target return if @target.empty? if klass delete(@target.select {|r| r.is_a? klass }) else @owner.send(@reflection.through_reflection.name).clear @target.clear end [] end
# File lib/has_many_polymorphs/association.rb, line 46 def construct_scope _logger_warn "Warning; not all usage scenarios for polymorphic scopes are supported yet." super end
Deletes a record from the association. Return value is undefined.
# File lib/has_many_polymorphs/association.rb, line 52 def delete(*records) records = flatten_deeper(records) records.reject! {|record| @target.delete(record) if record.new_record?} return if records.empty? @reflection.klass.transaction do records.each do |record| joins = @reflection.through_reflection.name @owner.send(joins).delete(@owner.send(joins).select do |join| join.send(@reflection.options[:polymorphic_key]) == record.id and join.send(@reflection.options[:polymorphic_type_key]) == "#{record.class.base_class}" end) @target.delete(record) end end end
Runs a find
against the association contents, returning the
matched records. All regular find
options except
:include
are supported.
# File lib/has_many_polymorphs/association.rb, line 40 def find(*args) opts = args._extract_options! opts.delete :include super(*(args + [opts])) end