Class: ActiveRecord::Associations::PolymorphicAssociation
The association class for a has_many_polymorphs association.
Aliases
| Old name | New name |
|---|---|
| construct_quoted_owner_attributes | construct_owner_attributes |
| custom_conditions | conditions |
| custom_conditions | sql_conditions |
Public Instance Methods
<< (*records)
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 14: def <<(*records) 15: return if records.empty? 16: 17: if @reflection.options[:skip_duplicates] 18: _logger_debug "Loading instances for polymorphic duplicate push check; use :skip_duplicates => false and perhaps a database constraint to avoid this possible performance issue" 19: load_target 20: end 21: 22: @reflection.klass.transaction do 23: flatten_deeper(records).each do |record| 24: if @owner.new_record? or not record.respond_to?(:new_record?) or record.new_record? 25: raise PolymorphicError, "You can't associate unsaved records." 26: end 27: next if @reflection.options[:skip_duplicates] and @target.include? record 28: @owner.send(@reflection.through_reflection.name).proxy_target << @reflection.klass.create!(construct_join_attributes(record)) 29: @target << record if loaded? 30: end 31: end 32: 33: self 34: end
clear (klass = nil)
Clears all records from the association. Returns an empty array.
# File lib/has_many_polymorphs/association.rb, line 70 70: def clear(klass = nil) 71: load_target 72: return if @target.empty? 73: 74: if klass 75: delete(@target.select {|r| r.is_a? klass }) 76: else 77: @owner.send(@reflection.through_reflection.name).clear 78: @target.clear 79: end 80: [] 81: end
concat (*records)
Alias for #<<
construct_scope ()
# File lib/has_many_polymorphs/association.rb, line 46 46: def construct_scope 47: _logger_warn "Warning; not all usage scenarios for polymorphic scopes are supported yet." 48: super 49: end
delete (*records)
Deletes a record from the association. Return value is undefined.
# File lib/has_many_polymorphs/association.rb, line 52 52: def delete(*records) 53: records = flatten_deeper(records) 54: records.reject! {|record| @target.delete(record) if record.new_record?} 55: return if records.empty? 56: 57: @reflection.klass.transaction do 58: records.each do |record| 59: joins = @reflection.through_reflection.name 60: @owner.send(joins).delete(@owner.send(joins).select do |join| 61: join.send(@reflection.options[:polymorphic_key]) == record.id and 62: join.send(@reflection.options[:polymorphic_type_key]) == "#{record.class.base_class}" 63: end) 64: @target.delete(record) 65: end 66: end 67: end
find (*args)
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 40: def find(*args) 41: opts = args._extract_options! 42: opts.delete :include 43: super(*(args + [opts])) 44: end
push (*records)
Alias for #<<