Class: ActiveRecord::Reflection::AssociationReflection

Inherits:
MacroReflection show all
Defined in:
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



194
195
196
# File 'lib/active_record/reflection.rb', line 194

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.



161
162
163
# File 'lib/active_record/reflection.rb', line 161

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

#check_validity!Object



214
215
# File 'lib/active_record/reflection.rb', line 214

def check_validity!
end

#columns(tbl_name, log_msg) ⇒ Object



206
207
208
# File 'lib/active_record/reflection.rb', line 206

def columns(tbl_name, log_msg)
  @columns ||= klass.connection.columns(tbl_name, log_msg)
end

#counter_cache_columnObject



198
199
200
201
202
203
204
# File 'lib/active_record/reflection.rb', line 198

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

#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.



168
169
170
# File 'lib/active_record/reflection.rb', line 168

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.



178
179
180
# File 'lib/active_record/reflection.rb', line 178

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.



155
156
157
# File 'lib/active_record/reflection.rb', line 155

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

#primary_key_nameObject



190
191
192
# File 'lib/active_record/reflection.rb', line 190

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

#quoted_table_nameObject



186
187
188
# File 'lib/active_record/reflection.rb', line 186

def quoted_table_name
  @quoted_table_name ||= klass.quoted_table_name
end

#reset_column_informationObject



210
211
212
# File 'lib/active_record/reflection.rb', line 210

def reset_column_information
  @columns = nil
end

#source_reflectionObject



224
225
226
# File 'lib/active_record/reflection.rb', line 224

def source_reflection
  nil
end

#table_nameObject



182
183
184
# File 'lib/active_record/reflection.rb', line 182

def table_name
  @table_name ||= klass.table_name
end

#through_reflectionObject



217
218
219
# File 'lib/active_record/reflection.rb', line 217

def through_reflection
  false
end

#through_reflection_primary_key_nameObject



221
222
# File 'lib/active_record/reflection.rb', line 221

def through_reflection_primary_key_name
end