Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/active_record/base.rb
Overview
:nodoc:
Class Method Summary collapse
- .belongs_to(association_name) ⇒ Object
- .has_many(association_name) ⇒ Object (also: has_and_belongs_to_many)
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #read_attribute(method_name) ⇒ Object
- #write_attribute(method_name, value) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 |
# File 'lib/active_record/base.rb', line 9 def initialize(attributes={}) attributes.each do |key, value| self.send "#{key}=".to_sym, value end end |
Class Method Details
.belongs_to(association_name) ⇒ Object
24 25 26 |
# File 'lib/active_record/base.rb', line 24 def belongs_to(association_name) attr_accessor association_name end |
.has_many(association_name) ⇒ Object Also known as: has_and_belongs_to_many
28 29 30 31 32 33 |
# File 'lib/active_record/base.rb', line 28 def has_many(association_name) attr_writer association_name define_method association_name do read_attribute(association_name) || write_attribute(association_name, []) end end |
Instance Method Details
#read_attribute(method_name) ⇒ Object
15 16 17 |
# File 'lib/active_record/base.rb', line 15 def read_attribute(method_name) instance_variable_get "@#{method_name}" end |
#write_attribute(method_name, value) ⇒ Object
19 20 21 |
# File 'lib/active_record/base.rb', line 19 def write_attribute(method_name, value) instance_variable_set "@#{method_name}", value end |