Module: Cequel::Record::Associations::ClassMethods
- Includes:
- Forwardable
- Defined in:
- lib/cequel/record/associations.rb
Overview
Class macros for declaring associations
Instance Attribute Summary collapse
-
#child_associations ⇒ Hash<Symbol,HasManyAssociation>
Associations declared by #has_many.
-
#parent_association ⇒ BelongsToAssociation
Association declared by #belongs_to.
Instance Method Summary collapse
-
#belongs_to(name, options = {}) ⇒ void
Declare the parent association for this record.
-
#has_many(name, options = {}) ⇒ void
Declare a child association.
Instance Attribute Details
#child_associations ⇒ Hash<Symbol,HasManyAssociation>
Returns associations declared by #has_many.
|
|
# File 'lib/cequel/record/associations.rb', line 69
|
#parent_association ⇒ BelongsToAssociation
Returns association declared by #belongs_to.
|
|
# File 'lib/cequel/record/associations.rb', line 69
|
Instance Method Details
#belongs_to(name, options = {}) ⇒ void
This method returns an undefined value.
Declare the parent association for this record. The name of the class is inferred from the name of the association. The belongs_to declaration also serves to define key columns, which are derived from the key columns of the parent class. So, if the parent class Blog has a primary key ‘(subdomain)`, this will declare a key column blog_subdomain of the same type.
Parent associations are read/write, so declaring ‘belongs_to :blog` will define a blog getter and blog= setter, which will update the underlying key column. Note that a record’s parent cannot be changed once the record has been saved.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cequel/record/associations.rb', line 96 def belongs_to(name, = {}) if parent_association fail InvalidRecordConfiguration, "Can't declare more than one belongs_to association" end if table_schema.key_columns.any? fail InvalidRecordConfiguration, "belongs_to association must be declared before declaring " \ "key(s)" end self.parent_association = BelongsToAssociation.new(self, name.to_sym, ) parent_association.association_key_columns.each do |column| key :"#{name}_#{column.name}", column.type end def_parent_association_accessors end |
#has_many(name, options = {}) ⇒ void
This method returns an undefined value.
Declare a child association. The child association should have a belongs_to referencing this class or, at a minimum, must have a primary key whose first N columns have the same types as the N columns in this class’s primary key.
has_many associations are read-only, so ‘has_many :posts` will define a posts reader but not a posts= writer; and the collection returned by posts will be immutable.
133 134 135 136 137 138 |
# File 'lib/cequel/record/associations.rb', line 133 def has_many(name, = {}) association = HasManyAssociation.new(self, name.to_sym, ) self.child_associations = child_associations.merge(name => association) def_child_association_reader(association) end |