class ActiveRecord::Reflection::AssociationReflection
:nodoc:
Active Record class.
Holds all the meta-data about an association as it was specified in the
def active_record_primary_key
def active_record_primary_key @active_record_primary_key ||= options[:primary_key] || primary_key(active_record) end
def association_class
def association_class case macro when :belongs_to if options[:polymorphic] Associations::BelongsToPolymorphicAssociation else Associations::BelongsToAssociation end when :has_and_belongs_to_many Associations::HasAndBelongsToManyAssociation when :has_many if options[:through] Associations::HasManyThroughAssociation else Associations::HasManyAssociation end when :has_one if options[:through] Associations::HasOneThroughAssociation else Associations::HasOneAssociation end end end
def association_foreign_key
def association_foreign_key @association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key end
def association_primary_key(klass = nil)
def association_primary_key(klass = nil) options[:primary_key] || primary_key(klass || self.klass) end
def belongs_to?
def belongs_to? macro == :belongs_to end
def build_association(attributes, &block)
Returns a new, unsaved instance of the associated class. +attributes+ will
def build_association(attributes, &block) klass.new(attributes, &block) end
def chain
A chain of reflections from this one back to the owner. For more see the explanation in
def chain [self] end
def check_validity!
def check_validity! check_validity_of_inverse! if has_and_belongs_to_many? && association_foreign_key == foreign_key raise HasAndBelongsToManyAssociationForeignKeyNeeded.new(self) end 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 either +has_many+ or
Returns whether or not this association reflection is for a collection
def collection? @collection end
def columns(tbl_name)
def columns(tbl_name) @columns ||= klass.connection.columns(tbl_name) 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].to_s end 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_foreign_key
def derive_foreign_key if belongs_to? "#{name}_id" elsif options[:as] "#{options[:as]}_id" else active_record.name.foreign_key end end
def derive_join_table
def derive_join_table [active_record.table_name, klass.table_name].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').gsub("\0", "_") end
def foreign_key
def foreign_key @foreign_key ||= options[:foreign_key] || derive_foreign_key end
def foreign_type
def foreign_type @foreign_type ||= options[:foreign_type] || "#{name}_type" end
def has_and_belongs_to_many?
def has_and_belongs_to_many? macro == :has_and_belongs_to_many end
def has_inverse?
def has_inverse? @options[:inverse_of] end
def initialize(*args)
def initialize(*args) super @collection = [:has_many, :has_and_belongs_to_many].include?(macro) end
def inverse_of
def inverse_of if has_inverse? @inverse_of ||= klass.reflect_on_association(options[:inverse_of]) end end
def join_table
def join_table @join_table ||= options[:join_table] || derive_join_table 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:
Active Record class.
Holds all the meta-data about an association as it was specified in the
def klass @klass ||= active_record.send(:compute_type, class_name) end
def nested?
def nested? false end
def polymorphic?
def polymorphic? options.key? :polymorphic 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(klass)
def primary_key(klass) klass.primary_key || raise(UnknownPrimaryKey.new(klass)) end
def primary_key_column
def primary_key_column @primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key } 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 scope_chain
An array of arrays of scopes. Each item in the outside array corresponds to a reflection
def scope_chain scope ? [[scope]] : [[]] 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 nil end
def type
def type @type ||= options[:as] && "#{options[:as]}_type" end
def validate?
* you use autosave; autosave: true
* you explicitly enable validation; validate: true
validate: false, validation will take place when:
Unless you explicitly 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