class ActiveRecord::Reflection::AssociationReflection
:nodoc:
Holds all the meta-data about an association as it was specified in the Active Record class.
def association_foreign_key
def association_foreign_key @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key end
def build_association(*options)
Returns a new, unsaved instance of the associated class. +options+ will
def build_association(*options) klass.new(*options) end
def check_validity!
def check_validity! check_validity_of_inverse! end
def check_validity_of_inverse!
def check_validity_of_inverse! unless options[:polymorphic] if has_inverse? && inverse_of.nil? raise InverseOfAssociationNotFoundError.new(self) end end end
def collection?
association. Returns +true+ if the +macro+ is one of +has_many+ or
Returns whether or not this association reflection is for a collection
def collection? if @collection.nil? @collection = [:has_many, :has_and_belongs_to_many].include?(macro) end @collection end
def columns(tbl_name, log_msg)
def columns(tbl_name, log_msg) @columns ||= klass.connection.columns(tbl_name, log_msg) end
def counter_cache_column
def counter_cache_column if options[:counter_cache] == true "#{active_record.name.demodulize.underscore.pluralize}_count" elsif options[:counter_cache] options[:counter_cache] end end
def create_association(*options)
with ActiveRecord::Base#save. +options+ will be passed to the class's
Creates a new instance of the associated class, and immediates saves it
def create_association(*options) klass.create(*options) end
def create_association!(*options)
exception will be raised.
creation method. If the created record doesn't pass validations, then an
with ActiveRecord::Base#save!. +options+ will be passed to the class's
Creates a new instance of the associated class, and immediates saves it
def create_association!(*options) klass.create!(*options) end
def dependent_conditions(record, base_class, extra_conditions)
def dependent_conditions(record, base_class, extra_conditions) dependent_conditions = [] dependent_conditions << "#{primary_key_name} = #{record.send(name).send(:owner_quoted_id)}" dependent_conditions << "#{options[:as]}_type = '#{base_class.name}'" if options[:as] dependent_conditions << klass.send(:sanitize_sql, options[:conditions]) if options[:conditions] dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ") dependent_conditions << extra_conditions if extra_conditions dependent_conditions = dependent_conditions.gsub('@', '\@') dependent_conditions end
def derive_class_name
def derive_class_name class_name = name.to_s.camelize class_name = class_name.singularize if collection? class_name end
def derive_primary_key_name
def derive_primary_key_name if belongs_to? "#{name}_id" elsif options[:as] "#{options[:as]}_id" else active_record.name.foreign_key end end
def has_inverse?
def has_inverse? !@options[:inverse_of].nil? end
def inverse_of
def inverse_of if has_inverse? @inverse_of ||= klass.reflect_on_association(options[:inverse_of]) end end
def klass
a new association object. Use +build_association+ or +create_association+
Note: do not call +klass.new+ or +klass.create+ to instantiate
# => Book
Author.reflect_on_association(:books).klass
end
has_many :books
class Author < ActiveRecord::Base
Returns the target association's class:
:nodoc:
Holds all the meta-data about an association as it was specified in the Active Record class.
def klass @klass ||= active_record.send(:compute_type, class_name) end
def polymorphic_inverse_of(associated_class)
def polymorphic_inverse_of(associated_class) if has_inverse? if inverse_relationship = associated_class.reflect_on_association(options[:inverse_of]) inverse_relationship else raise InverseOfAssociationNotFoundError.new(self, associated_class) end end end
def primary_key_name
def primary_key_name @primary_key_name ||= options[:foreign_key] || derive_primary_key_name end
def quoted_table_name
def quoted_table_name @quoted_table_name ||= klass.quoted_table_name end
def reset_column_information
def reset_column_information @columns = nil end
def source_reflection
def source_reflection nil end
def table_name
def table_name @table_name ||= klass.table_name end
def through_reflection
def through_reflection false end
def through_reflection_primary_key_name
def through_reflection_primary_key_name end
def validate?
* you use autosave; :autosave => true
* you explicitely enable validation; :validate => true
:validate => false, it will take place when:
Unless you explicitely disable validation with
the parent's validation.
Returns whether or not the association should be validated as part of
def validate? !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many) end