Class: ActiveRecord::Reflection::AssociationReflection

Inherits:
MacroReflection show all
Defined in:
lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb

Overview

Holds all the meta-data about an association as it was specified in the Active Record class.

Direct Known Subclasses

ThroughReflection

Instance Attribute Summary

Attributes inherited from MacroReflection

#active_record

Instance Method Summary collapse

Methods inherited from MacroReflection

#==, #belongs_to?, #class_name, #initialize, #macro, #name, #options, #sanitized_conditions

Constructor Details

This class inherits a constructor from ActiveRecord::Reflection::MacroReflection

Instance Method Details

#association_foreign_keyObject



189
190
191
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 189

def association_foreign_key
  @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
end

#build_association(*options) ⇒ Object

Returns a new, unsaved instance of the associated class. options will be passed to the class’s constructor.



156
157
158
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 156

def build_association(*options)
  klass.new(*options)
end

#check_validity!Object



201
202
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 201

def check_validity!
end

#counter_cache_columnObject



193
194
195
196
197
198
199
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 193

def counter_cache_column
  if options[:counter_cache] == true
    "#{active_record.name.underscore.pluralize}_count"
  elsif options[:counter_cache]
    options[:counter_cache]
  end
end

#create_association(*options) ⇒ Object

Creates a new instance of the associated class, and immediates saves it with ActiveRecord::Base#save. options will be passed to the class’s creation method. Returns the newly created object.



163
164
165
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 163

def create_association(*options)
  klass.create(*options)
end

#create_association!(*options) ⇒ Object

Creates a new instance of the associated class, and immediates 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.



173
174
175
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 173

def create_association!(*options)
  klass.create!(*options)
end

#klassObject

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.



150
151
152
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 150

def klass
  @klass ||= active_record.send(:compute_type, class_name)
end

#primary_key_nameObject



185
186
187
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 185

def primary_key_name
  @primary_key_name ||= options[:foreign_key] || derive_primary_key_name
end

#quoted_table_nameObject



181
182
183
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 181

def quoted_table_name
  @quoted_table_name ||= klass.quoted_table_name
end

#source_reflectionObject



211
212
213
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 211

def source_reflection
  nil
end

#table_nameObject



177
178
179
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 177

def table_name
  @table_name ||= klass.table_name
end

#through_reflectionObject



204
205
206
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 204

def through_reflection
  false
end

#through_reflection_primary_key_nameObject



208
209
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 208

def through_reflection_primary_key_name
end