Module: Cequel::Record::Associations::ClassMethods

Includes:
Util::Forwardable
Defined in:
lib/cequel/record/associations.rb

Overview

Class macros for declaring associations

See Also:

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Forwardable

#delegate

Instance Attribute Details

#child_associationsHash<Symbol,HasManyAssociation>

Returns associations declared by #has_many.

Returns:



# File 'lib/cequel/record/associations.rb', line 70

#parent_associationBelongsToAssociation

Returns association declared by #belongs_to.

Returns:



# 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.

Parameters:

  • name (Symbol)

    name of the parent association

  • options (Options) (defaults to: {})

    options for association

Options Hash (options):

  • :class_name (String)

    name of parent class

See Also:

Since:

  • 1.0.0



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, options = {})
  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

  key_options = options.extract!(:partition)

  self.parent_association =
    BelongsToAssociation.new(self, name.to_sym, options)

  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, key_options
  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.

Parameters:

  • name (Symbol)

    plural name of association

  • options (Options) (defaults to: {})

    options for association

Options Hash (options):

  • :class_name (Symbol)

    name of the child class

  • :dependent (Boolean)

    propagation behavior for destroy

See Also:

Since:

  • 1.0.0



142
143
144
145
146
147
# File 'lib/cequel/record/associations.rb', line 142

def has_many(name, options = {})
  association = HasManyAssociation.new(self, name.to_sym, options)
  self.child_associations =
    child_associations.merge(name => association)
  def_child_association_reader(association)
end