Class: ActiveRecord::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- MacroReflection
- ActiveRecord::Reflection::AssociationReflection
- 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
Instance Attribute Summary
Attributes inherited from MacroReflection
#active_record, #macro, #name, #options
Instance Method Summary collapse
- #association_foreign_key ⇒ Object
-
#belongs_to? ⇒ Boolean
Returns
true
ifself
is abelongs_to
reflection. -
#build_association(*options) ⇒ Object
Returns a new, unsaved instance of the associated class.
- #check_validity! ⇒ Object
- #check_validity_of_inverse! ⇒ Object
-
#collection? ⇒ Boolean
Returns whether or not this association reflection is for a collection association.
- #columns(tbl_name, log_msg) ⇒ Object
- #counter_cache_column ⇒ Object
-
#create_association(*options) ⇒ Object
Creates a new instance of the associated class, and immediately saves it with ActiveRecord::Base#save.
-
#create_association!(*options) ⇒ Object
Creates a new instance of the associated class, and immediately saves it with ActiveRecord::Base#save!.
- #dependent_conditions(record, base_class, extra_conditions) ⇒ Object
- #has_inverse? ⇒ Boolean
-
#initialize(macro, name, options, active_record) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
- #inverse_of ⇒ Object
-
#klass ⇒ Object
Returns the target association’s class.
- #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 ⇒ Object
-
#validate? ⇒ Boolean
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
#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, , active_record) super @collection = [:has_many, :has_and_belongs_to_many].include?(macro) end |
Instance Method Details
#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 |
#belongs_to? ⇒ Boolean
Returns true
if self
is a belongs_to
reflection.
304 305 306 |
# File 'lib/active_record/reflection.rb', line 304 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(*) klass.new(*) end |
#check_validity! ⇒ Object
229 230 231 |
# File 'lib/active_record/reflection.rb', line 229 def check_validity! check_validity_of_inverse! end |
#check_validity_of_inverse! ⇒ Object
233 234 235 236 237 238 239 |
# File '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 |
#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.
275 276 277 |
# File 'lib/active_record/reflection.rb', line 275 def collection? @collection end |
#columns(tbl_name, log_msg) ⇒ Object
221 222 223 |
# File 'lib/active_record/reflection.rb', line 221 def columns(tbl_name, log_msg) @columns ||= klass.connection.columns(tbl_name, log_msg) end |
#counter_cache_column ⇒ Object
213 214 215 216 217 218 219 |
# File '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 |
#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(*) klass.create(*) 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!(*) klass.create!(*) end |
#dependent_conditions(record, base_class, extra_conditions) ⇒ Object
292 293 294 295 296 297 298 299 300 301 |
# File '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 |
#has_inverse? ⇒ Boolean
252 253 254 |
# File 'lib/active_record/reflection.rb', line 252 def has_inverse? !@options[:inverse_of].nil? end |
#inverse_of ⇒ Object
256 257 258 259 260 |
# File 'lib/active_record/reflection.rb', line 256 def inverse_of if has_inverse? @inverse_of ||= klass.reflect_on_association([: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
# => 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 'lib/active_record/reflection.rb', line 161 def klass @klass ||= active_record.send(:compute_type, class_name) end |
#polymorphic_inverse_of(associated_class) ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File '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 |
#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 ||= [: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 |
#reset_column_information ⇒ Object
225 226 227 |
# File 'lib/active_record/reflection.rb', line 225 def reset_column_information @columns = nil end |
#source_reflection ⇒ Object
248 249 250 |
# File 'lib/active_record/reflection.rb', line 248 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
241 242 243 |
# File 'lib/active_record/reflection.rb', line 241 def through_reflection false end |
#through_reflection_primary_key_name ⇒ Object
245 246 |
# File 'lib/active_record/reflection.rb', line 245 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
288 289 290 |
# File 'lib/active_record/reflection.rb', line 288 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || macro == :has_many) end |