Module: Cequel::Record::Associations

Extended by:
ActiveSupport::Concern
Defined in:
lib/cequel/record/associations.rb

Overview

Cequel records can have parent-child relationships defined by belongs_to and has_many associations. Unlike in a relational database ORM, associations are not represented by foreign keys; instead they use CQL3’s compound primary keys. A child object’s primary key begins with it’s parent’s primary key.

In the below example, the ‘blogs` table has a one-column primary key `(subdomain)`, and the `posts` table has a two-column primary key `(blog_subdomain, permalink)`. All posts that belong to the blog with subdomain `“cassandra”` will have `“cassandra”` as their `blog_subdomain`.

Examples:

Blogs and Posts


class Blog
  include Cequel::Record

  key :subdomain, :text

  column :name, :text

  has_many :posts
end

class Post
  include Cequel::Record

  # This defines the first primary key column as `blog_subdomain`.
  # Because `belongs_to` associations implicitly define columns in the
  # primary key, it must come before any explicit key definition. For
  # the same reason, a Record class can only have a single `belongs_to`
  # declaration.
  belongs_to :blog

  # We also define an additional primary key column so that each post
  # has a unique compound primary key
  key :permalink

  column :title, :text
  column :body, :text
end

blog = Blog.new(subdomain: 'cassandra')
post = blog.posts.new(permalink: 'cequel')
post.blog_subdomain #=> "cassandra"

Since:

  • 1.0.0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject

Since:

  • 1.0.0



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cequel/record/associations.rb', line 178

def destroy(*)
  super.tap do
    self.class.child_associations.each_value do |association|
      case association.dependent
      when :destroy
        __send__(association.name).destroy_all
      when :delete
        __send__(association.name).delete_all
      end
    end
  end
end