Class: ActiveRecord::Reflection::AssociationReflection
Overview
Holds all the meta-data about an association as it was specified in the Active Record class.
Instance Attribute Summary
#active_record, #macro, #name, #options
Instance Method Summary
collapse
#==, #class_name, #sanitized_conditions
Constructor Details
#initialize(macro, name, options, active_record) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
165
166
167
168
|
# File 'lib/active_record/reflection.rb', line 165
def initialize(macro, name, options, active_record)
super
@collection = [:has_many, :has_and_belongs_to_many].include?(macro)
end
|
Instance Method Details
#active_record_primary_key ⇒ Object
217
218
219
|
# File 'lib/active_record/reflection.rb', line 217
def active_record_primary_key
@active_record_primary_key ||= options[:primary_key] || active_record.primary_key
end
|
#association_foreign_key ⇒ Object
209
210
211
|
# File 'lib/active_record/reflection.rb', line 209
def association_foreign_key
@association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key
end
|
#association_primary_key ⇒ Object
213
214
215
|
# File 'lib/active_record/reflection.rb', line 213
def association_primary_key
@association_primary_key ||= options[:primary_key] || klass.primary_key
end
|
#belongs_to? ⇒ Boolean
Returns true
if self
is a belongs_to
reflection.
312
313
314
|
# File 'lib/active_record/reflection.rb', line 312
def belongs_to?
macro == :belongs_to
end
|
#build_association(*options) ⇒ Object
Returns a new, unsaved instance of the associated class. options
will be passed to the class’s constructor.
172
173
174
|
# File 'lib/active_record/reflection.rb', line 172
def build_association(*options)
klass.new(*options)
end
|
#check_validity! ⇒ Object
237
238
239
|
# File 'lib/active_record/reflection.rb', line 237
def check_validity!
check_validity_of_inverse!
end
|
#check_validity_of_inverse! ⇒ Object
241
242
243
244
245
246
247
|
# File 'lib/active_record/reflection.rb', line 241
def check_validity_of_inverse!
unless options[:polymorphic]
if has_inverse? && inverse_of.nil?
raise InverseOfAssociationNotFoundError.new(self)
end
end
end
|
#collection? ⇒ Boolean
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.
283
284
285
|
# File 'lib/active_record/reflection.rb', line 283
def collection?
@collection
end
|
#columns(tbl_name, log_msg) ⇒ Object
229
230
231
|
# File 'lib/active_record/reflection.rb', line 229
def columns(tbl_name, log_msg)
@columns ||= klass.connection.columns(tbl_name, log_msg)
end
|
#counter_cache_column ⇒ Object
221
222
223
224
225
226
227
|
# File 'lib/active_record/reflection.rb', line 221
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 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 'lib/active_record/reflection.rb', line 179
def create_association(*options)
klass.create(*options)
end
|
#create_association!(*options) ⇒ Object
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 'lib/active_record/reflection.rb', line 189
def create_association!(*options)
klass.create!(*options)
end
|
#dependent_conditions(record, base_class, extra_conditions) ⇒ Object
300
301
302
303
304
305
306
307
308
309
|
# File 'lib/active_record/reflection.rb', line 300
def dependent_conditions(record, base_class, )
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 << if
dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
dependent_conditions = dependent_conditions.gsub('@', '\@')
dependent_conditions
end
|
#has_inverse? ⇒ Boolean
260
261
262
|
# File 'lib/active_record/reflection.rb', line 260
def has_inverse?
!@options[:inverse_of].nil?
end
|
#inverse_of ⇒ Object
264
265
266
267
268
|
# File 'lib/active_record/reflection.rb', line 264
def inverse_of
if has_inverse?
@inverse_of ||= klass.reflect_on_association(options[:inverse_of])
end
end
|
#klass ⇒ Object
Returns the target association’s class.
class Author < ActiveRecord::Base
has_many :books
end
Author.reflect_on_association(:books).klass
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 'lib/active_record/reflection.rb', line 161
def klass
@klass ||= active_record.send(:compute_type, class_name)
end
|
#polymorphic_inverse_of(associated_class) ⇒ Object
270
271
272
273
274
275
276
277
278
|
# File 'lib/active_record/reflection.rb', line 270
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
|
#primary_key_column ⇒ Object
205
206
207
|
# File 'lib/active_record/reflection.rb', line 205
def primary_key_column
@primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key }
end
|
#primary_key_name ⇒ Object
201
202
203
|
# File 'lib/active_record/reflection.rb', line 201
def primary_key_name
@primary_key_name ||= options[:foreign_key] || derive_primary_key_name
end
|
#quoted_table_name ⇒ Object
197
198
199
|
# File 'lib/active_record/reflection.rb', line 197
def quoted_table_name
@quoted_table_name ||= klass.quoted_table_name
end
|
233
234
235
|
# File 'lib/active_record/reflection.rb', line 233
def reset_column_information
@columns = nil
end
|
#source_reflection ⇒ Object
256
257
258
|
# File 'lib/active_record/reflection.rb', line 256
def source_reflection
nil
end
|
#table_name ⇒ Object
193
194
195
|
# File 'lib/active_record/reflection.rb', line 193
def table_name
@table_name ||= klass.table_name
end
|
#through_reflection ⇒ Object
249
250
251
|
# File 'lib/active_record/reflection.rb', line 249
def through_reflection
false
end
|
#through_reflection_primary_key_name ⇒ Object
253
254
|
# File 'lib/active_record/reflection.rb', line 253
def through_reflection_primary_key_name
end
|
#validate? ⇒ Boolean
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
296
297
298
|
# File 'lib/active_record/reflection.rb', line 296
def validate?
!options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
end
|