Class: ActiveRecord::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- MacroReflection
- ActiveRecord::Reflection::AssociationReflection
- Defined in:
- activerecord/lib/active_record/reflection.rb
Overview
Holds all the meta-data about an association as it was specified in the Active Record class.
Instance Attribute Summary
Attributes inherited from MacroReflection
#active_record, #macro, #name, #options
Instance Method Summary (collapse)
- - (Object) association_foreign_key
-
- (Boolean) belongs_to?
Returns true if self is a belongs_to reflection.
-
- (Object) build_association(*options)
Returns a new, unsaved instance of the associated class.
- - (Object) check_validity!
- - (Object) check_validity_of_inverse!
-
- (Boolean) collection?
Returns whether or not this association reflection is for a collection association.
- - (Object) columns(tbl_name, log_msg)
- - (Object) counter_cache_column
-
- (Object) create_association(*options)
Creates a new instance of the associated class, and immediately saves it with ActiveRecord::Base#save.
-
- (Object) create_association!(*options)
Creates a new instance of the associated class, and immediately saves it with ActiveRecord::Base#save!.
- - (Object) dependent_conditions(record, base_class, extra_conditions)
- - (Boolean) has_inverse?
-
- (AssociationReflection) initialize(macro, name, options, active_record)
constructor
A new instance of AssociationReflection.
- - (Object) inverse_of
-
- (Object) klass
Returns the target association's class.
- - (Object) polymorphic_inverse_of(associated_class)
- - (Object) primary_key_column
- - (Object) primary_key_name
- - (Object) quoted_table_name
- - (Object) reset_column_information
- - (Object) source_reflection
- - (Object) table_name
- - (Object) through_reflection
- - (Object) through_reflection_primary_key_name
-
- (Boolean) validate?
Returns whether or not the association should be validated as part of the parent's validation.
Methods inherited from MacroReflection
#==, #class_name, #sanitized_conditions
Constructor Details
- (AssociationReflection) initialize(macro, name, options, active_record)
A new instance of AssociationReflection
165 166 167 168 |
# File 'activerecord/lib/active_record/reflection.rb', line 165 def initialize(macro, name, , active_record) super @collection = [:has_many, :has_and_belongs_to_many].include?(macro) end |
Instance Method Details
- (Object) association_foreign_key
209 210 211 |
# File 'activerecord/lib/active_record/reflection.rb', line 209 def association_foreign_key @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key end |
- (Boolean) belongs_to?
Returns true if self is a belongs_to reflection.
304 305 306 |
# File 'activerecord/lib/active_record/reflection.rb', line 304 def belongs_to? macro == :belongs_to end |
- (Object) build_association(*options)
Returns a new, unsaved instance of the associated class. options will be passed to the class's constructor.
172 173 174 |
# File 'activerecord/lib/active_record/reflection.rb', line 172 def build_association(*) klass.new(*) end |
- (Object) check_validity!
229 230 231 |
# File 'activerecord/lib/active_record/reflection.rb', line 229 def check_validity! check_validity_of_inverse! end |
- (Object) check_validity_of_inverse!
233 234 235 236 237 238 239 |
# File 'activerecord/lib/active_record/reflection.rb', line 233 def check_validity_of_inverse! unless [:polymorphic] if has_inverse? && inverse_of.nil? raise InverseOfAssociationNotFoundError.new(self) end end end |
- (Boolean) collection?
Returns whether or not this association reflection is for a collection association. Returns true if the macro is either has_many or has_and_belongs_to_many, false otherwise.
275 276 277 |
# File 'activerecord/lib/active_record/reflection.rb', line 275 def collection? @collection end |
- (Object) columns(tbl_name, log_msg)
221 222 223 |
# File 'activerecord/lib/active_record/reflection.rb', line 221 def columns(tbl_name, log_msg) @columns ||= klass.connection.columns(tbl_name, log_msg) end |
- (Object) counter_cache_column
213 214 215 216 217 218 219 |
# File 'activerecord/lib/active_record/reflection.rb', line 213 def counter_cache_column if [:counter_cache] == true "#{active_record.name.demodulize.underscore.pluralize}_count" elsif [:counter_cache] [:counter_cache] end end |
- (Object) create_association(*options)
Creates a new instance of the associated class, and immediately saves it with ActiveRecord::Base#save. options will be passed to the class's creation method. Returns the newly created object.
179 180 181 |
# File 'activerecord/lib/active_record/reflection.rb', line 179 def create_association(*) klass.create(*) end |
- (Object) create_association!(*options)
Creates a new instance of the associated class, and immediately saves it with ActiveRecord::Base#save!. options will be passed to the class's creation method. If the created record doesn't pass validations, then an exception will be raised.
Returns the newly created object.
189 190 191 |
# File 'activerecord/lib/active_record/reflection.rb', line 189 def create_association!(*) klass.create!(*) end |
- (Object) dependent_conditions(record, base_class, extra_conditions)
292 293 294 295 296 297 298 299 300 301 |
# File 'activerecord/lib/active_record/reflection.rb', line 292 def dependent_conditions(record, base_class, extra_conditions) dependent_conditions = [] dependent_conditions << "#{primary_key_name} = #{record.send(name).send(:owner_quoted_id)}" dependent_conditions << "#{[:as]}_type = '#{base_class.name}'" if [:as] dependent_conditions << klass.send(:sanitize_sql, [:conditions]) if [:conditions] dependent_conditions << extra_conditions if extra_conditions dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ") dependent_conditions = dependent_conditions.gsub('@', '\@') dependent_conditions end |
- (Boolean) has_inverse?
252 253 254 |
# File 'activerecord/lib/active_record/reflection.rb', line 252 def has_inverse? !@options[:inverse_of].nil? end |
- (Object) inverse_of
256 257 258 259 260 |
# File 'activerecord/lib/active_record/reflection.rb', line 256 def inverse_of if has_inverse? @inverse_of ||= klass.reflect_on_association([:inverse_of]) end end |
- (Object) klass
Returns the target association's class.
class Author < ActiveRecord::Base
has_many :books
end
Author.reflect_on_association(:books).klass
# => Book
Note: Do not call klass.new or klass.create to instantiate a new association object. Use build_association or create_association instead. This allows plugins to hook into association object creation.
161 162 163 |
# File 'activerecord/lib/active_record/reflection.rb', line 161 def klass @klass ||= active_record.send(:compute_type, class_name) end |
- (Object) polymorphic_inverse_of(associated_class)
262 263 264 265 266 267 268 269 270 |
# File 'activerecord/lib/active_record/reflection.rb', line 262 def polymorphic_inverse_of(associated_class) if has_inverse? if inverse_relationship = associated_class.reflect_on_association([:inverse_of]) inverse_relationship else raise InverseOfAssociationNotFoundError.new(self, associated_class) end end end |
- (Object) primary_key_column
205 206 207 |
# File 'activerecord/lib/active_record/reflection.rb', line 205 def primary_key_column @primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key } end |
- (Object) primary_key_name
201 202 203 |
# File 'activerecord/lib/active_record/reflection.rb', line 201 def primary_key_name @primary_key_name ||= [:foreign_key] || derive_primary_key_name end |
- (Object) quoted_table_name
197 198 199 |
# File 'activerecord/lib/active_record/reflection.rb', line 197 def quoted_table_name @quoted_table_name ||= klass.quoted_table_name end |
- (Object) reset_column_information
225 226 227 |
# File 'activerecord/lib/active_record/reflection.rb', line 225 def reset_column_information @columns = nil end |
- (Object) source_reflection
248 249 250 |
# File 'activerecord/lib/active_record/reflection.rb', line 248 def source_reflection nil end |
- (Object) table_name
193 194 195 |
# File 'activerecord/lib/active_record/reflection.rb', line 193 def table_name @table_name ||= klass.table_name end |
- (Object) through_reflection
241 242 243 |
# File 'activerecord/lib/active_record/reflection.rb', line 241 def through_reflection false end |
- (Object) through_reflection_primary_key_name
245 246 |
# File 'activerecord/lib/active_record/reflection.rb', line 245 def through_reflection_primary_key_name end |
- (Boolean) validate?
Returns whether or not the association should be validated as part of the parent's validation.
Unless you explicitly disable validation with :validate => false, validation will take place when:
-
you explicitly enable validation; :validate => true
-
you use autosave; :autosave => true
-
the association is a has_many association
288 289 290 |
# File 'activerecord/lib/active_record/reflection.rb', line 288 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || macro == :has_many) end |