Module: Cequel::Record::Associations::ClassMethods
- Includes:
- Util::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.
Methods included from Util::Forwardable
Instance Attribute Details
#child_associations ⇒ Hash<Symbol,HasManyAssociation>
Returns associations declared by #has_many.
|
# File 'lib/cequel/record/associations.rb', line 70
|
#parent_association ⇒ BelongsToAssociation
Returns association declared by #belongs_to.
|
# File 'lib/cequel/record/associations.rb', line 70
|
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.
If the parent class has multiple keys, e.g. it belongs to a parent class, defining a ‘partition: true` option will declare all of the parent’s keys as partition key columns for this class.
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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cequel/record/associations.rb', line 101 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 = .extract!(:partition) self.parent_association = BelongsToAssociation.new(self, name.to_sym, ) parent_association.association_key_columns.each_with_index do |column, i| foreign_key_parts = self.parent_association.foreign_keys foreign_key = foreign_key_parts.any? ? foreign_key_parts[i] : "#{name}_#{column.name}" key foreign_key.to_sym, 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.
142 143 144 145 146 147 |
# File 'lib/cequel/record/associations.rb', line 142 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 |