Class: ActiveRecord::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- MacroReflection
- ActiveRecord::Reflection::AssociationReflection
- 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
Instance Attribute Summary
Attributes inherited from MacroReflection
Instance Method Summary collapse
- #association_foreign_key ⇒ Object
-
#build_association(*options) ⇒ Object
Returns a new, unsaved instance of the associated class.
- #check_validity! ⇒ Object
- #counter_cache_column ⇒ Object
-
#create_association(*options) ⇒ Object
Creates a new instance of the associated class, and immediates saves it with ActiveRecord::Base#save.
-
#create_association!(*options) ⇒ Object
Creates a new instance of the associated class, and immediates saves it with ActiveRecord::Base#save!.
-
#klass ⇒ Object
Returns the target association’s class:.
- #primary_key_name ⇒ Object
- #quoted_table_name ⇒ Object
- #source_reflection ⇒ Object
- #table_name ⇒ Object
- #through_reflection ⇒ Object
- #through_reflection_primary_key_name ⇒ Object
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_key ⇒ Object
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(*) klass.new(*) 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_column ⇒ Object
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 [:counter_cache] == true "#{active_record.name.underscore.pluralize}_count" elsif [:counter_cache] [: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(*) klass.create(*) 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!(*) klass.create!(*) 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.
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_name ⇒ Object
185 186 187 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 185 def primary_key_name @primary_key_name ||= [:foreign_key] || derive_primary_key_name end |
#quoted_table_name ⇒ Object
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_reflection ⇒ Object
211 212 213 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 211 def source_reflection nil end |
#table_name ⇒ Object
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_reflection ⇒ Object
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_name ⇒ Object
208 209 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 208 def through_reflection_primary_key_name end |