module Cequel::Record::Associations::ClassMethods
Class macros for declaring associations
@see Associations
Public Instance Methods
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.
@param name [Symbol] name of the parent association @param options [Options] options for association @option (see BelongsToAssociation#initialize) @return [void]
@see Associations
# File lib/cequel/record/associations.rb, line 100 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
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.
@param name [Symbol] plural name of association @param options [Options] options for association @option (see HasManyAssociation#initialize) @return [void]
@see Associations
# File lib/cequel/record/associations.rb, line 141 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
Private Instance Methods
# File lib/cequel/record/associations.rb, line 165 def def_child_association_reader(association) module_eval <<-RUBY, __FILE__, __LINE__+1 def #{association.name}(reload = false) read_child_association(#{association.name.inspect}, reload) end RUBY end
# File lib/cequel/record/associations.rb, line 150 def def_parent_association_accessors def_parent_association_reader def_parent_association_writer end
# File lib/cequel/record/associations.rb, line 155 def def_parent_association_reader def_delegator 'self', :read_parent_association, parent_association.name end
# File lib/cequel/record/associations.rb, line 160 def def_parent_association_writer def_delegator 'self', :write_parent_association, "#{parent_association.name}=" end