Skip to content
Spencer Steffen edited this page Feb 11, 2015 · 6 revisions

General

cdq.setup                # Load the whole system

cdq.contexts.current     # The currently-active NSManagedObjectContext
cdq.contexts.all         # See all contexts on the stack
cdq.contexts.new(type)   # Create a new context and push it onto the stack

cdq.save                 # Save all contexts on the stack

Object Lifecycle

Creating

  Author.create(name: "Le Guin", publish_count: 150, first_published: 1970)
  Author.create(name: "Shakespeare", publish_count: 400, first_published: 1550)
  Author.create(name: "Blake", publish_count: 100, first_published: 1778)
  cdq.save

Updating

  author = Author.first
  author.name = "Ursula K. Le Guin"
  cdq.save

Deleting

  author = Author.first
  author.destroy
  cdq.save

Queries

Author.where(:name).eq("Emily")
Author.where(:name).not_equal("Emily")
Author.limit(1)
Author.offset(10)
Author.where(:name).contains("A").offset(10).first

# Conjuctions
Author.where(:name).contains("Emily").and.contains("Dickinson")
Author.where(:name).begins_with("E").or(:pub_count).eq(1)
Post.where(:date).ge(start_date).and.le(end_date) # gt, ge (greater or equal), lt, le (less or equal)

# Nested Conjuctions
Author.where(:name).contains("Emily").and(cdq(:pub_count).gt(100).or.lt(10))

# Relationships
Author.first.publications.offset(2).limit(1)
cdq(emily_dickinson).publications.where(:type).eq('poetry')

# Sorting
Author.sort_by(:name)
Author.sort_by(:name, order: :descending, case_insensitive: true)

Scopes

class Author < CDQManagedObject
  scope :prolific, where(:pub_count).gt(50)
end

Operators

Many short-form operators also have verbose equivalents.

eq (equal)
ne (not_equal)
lt (less_than)
le (less_than_or_equal)
gt (greater_than)
ge (greater_than_or_equal)
contains
matches
in
begins_with
ends_with
between

Accessors

These methods pull you out of CDQ-land and return actual objects or values. They go at the end of a query or scope and will force execution.

array (to_a)
first
map
each
[]