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
Instance Method Summary
collapse
#==, #belongs_to?, #class_name, #initialize, #macro, #name, #options, #sanitized_conditions
Instance Method Details
#association_foreign_key ⇒ Object
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
216
|
# File 'lib/active_record/reflection.rb', line 214
def check_validity!
check_validity_of_inverse!
end
|
#check_validity_of_inverse! ⇒ Object
218
219
220
221
222
223
224
|
# File 'lib/active_record/reflection.rb', line 218
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 one of has_many
or has_and_belongs_to_many
, false
otherwise.
260
261
262
263
264
265
|
# File 'lib/active_record/reflection.rb', line 260
def collection?
if @collection.nil?
@collection = [:has_many, :has_and_belongs_to_many].include?(macro)
end
@collection
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_column ⇒ Object
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
|
#dependent_conditions(record, base_class, extra_conditions) ⇒ Object
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/active_record/reflection.rb', line 280
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 = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
dependent_conditions << if
dependent_conditions = dependent_conditions.gsub('@', '\@')
dependent_conditions
end
|
#has_inverse? ⇒ Boolean
237
238
239
|
# File 'lib/active_record/reflection.rb', line 237
def has_inverse?
!@options[:inverse_of].nil?
end
|
#inverse_of ⇒ Object
241
242
243
244
245
|
# File 'lib/active_record/reflection.rb', line 241
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.
155
156
157
|
# File 'lib/active_record/reflection.rb', line 155
def klass
@klass ||= active_record.send(:compute_type, class_name)
end
|
#polymorphic_inverse_of(associated_class) ⇒ Object
247
248
249
250
251
252
253
254
255
|
# File 'lib/active_record/reflection.rb', line 247
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_name ⇒ Object
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_name ⇒ Object
186
187
188
|
# File 'lib/active_record/reflection.rb', line 186
def quoted_table_name
@quoted_table_name ||= klass.quoted_table_name
end
|
210
211
212
|
# File 'lib/active_record/reflection.rb', line 210
def reset_column_information
@columns = nil
end
|
#source_reflection ⇒ Object
233
234
235
|
# File 'lib/active_record/reflection.rb', line 233
def source_reflection
nil
end
|
#table_name ⇒ Object
182
183
184
|
# File 'lib/active_record/reflection.rb', line 182
def table_name
@table_name ||= klass.table_name
end
|
#through_reflection ⇒ Object
226
227
228
|
# File 'lib/active_record/reflection.rb', line 226
def through_reflection
false
end
|
#through_reflection_primary_key_name ⇒ Object
230
231
|
# File 'lib/active_record/reflection.rb', line 230
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 explicitely disable validation with :validate => false
, it will take place when:
-
you explicitely enable validation; :validate => true
-
you use autosave; :autosave => true
-
the association is a has_many
association
276
277
278
|
# File 'lib/active_record/reflection.rb', line 276
def validate?
!options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
end
|