Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Fetching latest commit…

Cannot retrieve the latest commit at this time

..
Failed to load latest commit information.
lib
spec
CHANGELOG
COPYING
README
Rakefile

README

== Sequel Models

Models in Sequel are based on the Active Record pattern described by Martin Fowler (http://www.martinfowler.com/eaaCatalog/activeRecord.html). A model class corresponds to a table or a dataset, and an instance of that class wraps a single record in the model's underlying dataset.

Model classes are defined as regular Ruby classes:

  DB = Sequel.connect('sqlite:/blog.db')
  class Post < Sequel::Model
  end

Just like in DataMapper or ActiveRecord, Sequel model classes assume that the table name is a plural of the class name:

  Post.table_name #=> :posts

You can, however, explicitly set the table name or even the dataset used:

  class Post < Sequel::Model(:my_posts)
  end
  # or:
  Post.set_dataset :my_posts
  # or:
  Post.set_dataset DB[:my_posts].where(:category => 'ruby')

=== Resources

* {Source code}[http://github.com/jeremyevans/sequel]
* {Bug tracking}[http://code.google.com/p/ruby-sequel/issues/list]
* {Google group}[http://groups.google.com/group/sequel-talk]
* {RubyForge page}[http://rubyforge.org/projects/sequel/]
* {API RDoc}[http://sequel.rubyforge.org]

To check out the source code:

  git clone git://github.com/jeremyevans/sequel.git

=== Contact

If you have any comments or suggestions please post to the Google group.

=== Installation

  sudo gem install sequel

=== Model instances

Model instance are identified by a primary key. By default, Sequel assumes the primary key column to be :id. The Model#[] method can be used to fetch records by their primary key:

  post = Post[123]

The Model#pk method is used to retrieve the record's primary key value:

  post.pk #=> 123

Sequel models allow you to use any column as a primary key, and even composite keys made from multiple columns:

  class Post < Sequel::Model
    set_primary_key [:category, :title]
  end

  post = Post['ruby', 'hello world']
  post.pk #=> ['ruby', 'hello world']

You can also define a model class that does not have a primary key, but then you lose the ability to update records.

A model instance can also be fetched by specifying a condition:

  post = Post[:title => 'hello world']
  post = Post.find(:num_comments < 10)

=== Iterating over records

A model class lets you iterate over specific records by acting as a proxy to the underlying dataset. This means that you can use the entire Dataset API to create customized queries that return model instances, e.g.:

  Post.filter(:category => 'ruby').each{|post| p post}

You can also manipulate the records in the dataset:

  Post.filter(:num_comments < 7).delete
  Post.filter(:title.like(/ruby/)).update(:category => 'ruby')

=== Accessing record values

A model instances stores its values as a hash:

  post.values #=> {:id => 123, :category => 'ruby', :title => 'hello world'}

You can read the record values as object attributes (assuming the attribute names are valid columns in the model's dataset):

  post.id #=> 123
  post.title #=> 'hello world'

You can also change record values:

  post.title = 'hey there'
  post.save

Another way to change values by using the #update_with_params method:

  post.update_with_params(:title => 'hey there')

=== Creating new records

New records can be created by calling Model.create:

  post = Post.create(:title => 'hello world')

Another way is to construct a new instance and save it:

  post = Post.new
  post.title = 'hello world'
  post.save

You can also supply a block to Model.new and Model.create:

  post = Post.create {|p| p.title = 'hello world'}

  post = Post.new do |p|
    p.title = 'hello world'
    p.save
  end

=== Hooks

You can execute custom code when creating, updating, or deleting records by using hooks. The before_create and after_create hooks wrap record creation. The before_update and after_update wrap record updating. The before_save and after_save wrap record creation and updating. The before_destroy and after_destroy wrap destruction. The before_validation and after_validation hooks wrap validation.

Hooks are defined by supplying a block:

  class Post < Sequel::Model
    after_create do
      self.created_at = Time.now
    end

    after_destroy do
      author.update_post_count
    end
  end

=== Deleting records

You can delete individual records by calling #delete or #destroy. The only difference between the two methods is that #destroy invokes before_destroy and after_destroy hooks, while #delete does not:

  post.delete #=> bypasses hooks
  post.destroy #=> runs hooks

Records can also be deleted en-masse by invoking Model.delete and Model.destroy. As stated above, you can specify filters for the deleted records:

  Post.filter(:category => 32).delete #=> bypasses hooks
  Post.filter(:category => 32).destroy #=> runs hooks

Please note that if Model.destroy is called, each record is deleted 
separately, but Model.delete deletes all relevant records with a single 
SQL statement.

=== Associations

Associations are used in order to specify relationships between model classes that reflect relations between tables in the database using foreign keys.

  class Post < Sequel::Model
    many_to_one :author
    one_to_many :comments
    many_to_many :tags
  end

You can also use the ActiveRecord names for these associations:

  class Post < Sequel::Model
    belongs_to :author
    has_many :comments
    has_and_belongs_to_many :tags
  end

many_to_one creates a getter and setter for each model object:

  class Post < Sequel::Model
    many_to_one :author
  end

  post = Post.create(:name => 'hi!')
  post.author = Author[:name => 'Sharon']
  post.author

one_to_many and many_to_many create a getter method, a method for adding an object to the association, a method for removing an object from the association, and a method for removing all associated objected from the association:

  class Post < Sequel::Model
    one_to_many :comments
    many_to_many :tags
  end

  post = Post.create(:name => 'hi!')
  post.comments
  comment = Comment.create(:text=>'hi')
  post.add_comment(comment)
  post.remove_comment(comment)
  post.remove_all_comments
  tag = Tag.create(:tag=>'interesting')
  post.add_tag(tag)
  post.remove_tag(tag)
  post.remove_all_tags

=== Eager Loading

Associations can be eagerly loaded via .eager and the :eager association option. Eager loading is used when loading a group of objects. It loads all associated objects for all of the current objects in one query, instead of using a separate query to get the associated objects for each current object. Eager loading requires that you retrieve all model objects at once via .all (instead of individually by .each). Eager loading can be cascaded, loading association's associated objects.

  class Person < Sequel::Model
    one_to_many :posts, :eager=>[:tags]
  end

  class Post < Sequel::Model
    many_to_one :person
    one_to_many :replies
    many_to_many :tags
  end

  class Tag < Sequel::Model
    many_to_many :posts
    many_to_many :replies
  end

  class Reply < Sequel::Model
    many_to_one :person
    many_to_one :post
    many_to_many :tags
  end

  # Eager loading via .eager
  Post.eager(:person).all

  # eager is a dataset method, so it works with filters/orders/limits/etc.
  Post.filter(:topic > 'M').order(:date).limit(5).eager(:person).all
  
  person = Person.first
  # Eager loading via :eager (will eagerly load the tags for this person's posts)
  person.posts
  
  # These are equivalent
  Post.eager(:person, :tags).all
  Post.eager(:person).eager(:tags).all
  
  # Cascading via .eager
  Tag.eager(:posts=>:replies).all
  
  # Will also grab all associated posts' tags (because of :eager)
  Reply.eager(:person=>:posts).all
  
  # No depth limit (other than memory/stack), and will also grab posts' tags
  # Loads all people, their posts, their posts' tags, replies to those posts,
  # the person for each reply, the tag for each reply, and all posts and
  # replies that have that tag.  Uses a total of 8 queries.
  Person.eager(:posts=>{:replies=>[:person, {:tags=>{:posts, :replies}}]}).all

In addition to using eager, you can also use eager_graph, which will use a single query to get the object and all associated objects.  This may be necessary if you want to filter the result set based on columns in associated tables.  It works with cascading as well, the syntax is exactly the same.  Note that using eager_graph to eagerly load multiple *_to_many associations will cause the result set to be a cartesian product, so you should be very careful with your filters when using it in that case.

=== Caching model instances with memcached

Sequel models can be cached using memcached based on their primary keys. The use of memcached can significantly reduce database load by keeping model instances in memory. The set_cache method is used to specify caching:

  require 'memcache'
  CACHE = MemCache.new 'localhost:11211', :namespace => 'blog'

  class Author < Sequel::Model
    set_cache CACHE, :ttl => 3600
  end

  Author[333] # database hit
  Author[333] # cache hit

=== Extending the underlying dataset

The obvious way to add table-wide logic is to define class methods to the model class definition. That way you can define subsets of the underlying dataset, change the ordering, or perform actions on multiple records:

  class Post < Sequel::Model
    def self.posts_with_few_comments
      filter(:num_comments < 30)
    end

    def self.clean_posts_with_few_comments
      posts_with_few_comments.delete
    end
  end

You can also implement table-wide logic by defining methods on the dataset:

  class Post < Sequel::Model
    def_dataset_method(:posts_with_few_comments) do
      filter(:num_comments < 30)
    end

    def_dataset_method(:clean_posts_with_few_comments) do
      posts_with_few_comments.delete
    end
  end

This is the recommended way of implementing table-wide operations, and allows you to have access to your model API from filtered datasets as well:

  Post.filter(:category => 'ruby').clean_old_posts

Sequel models also provide a short hand notation for filters:

  class Post < Sequel::Model
    subset(:posts_with_few_comments, :num_comments < 30)
    subset :invisible, :visible => false
  end

=== Defining the underlying schema

Model classes can also be used as a place to define your table schema and control it. The schema DSL is exactly the same provided by Sequel::Schema::Generator:

  class Post < Sequel::Model
    set_schema do
      primary_key :id
      text :title
      text :category
      foreign_key :author_id, :table => :authors
    end
  end

You can then create the underlying table, drop it, or recreate it:

  Post.table_exists?
  Post.create_table
  Post.drop_table
  Post.create_table! # drops the table if it exists and then recreates it

=== Basic Model Validations

To assign default validations to a sequel model:

  class MyModel < Sequel::Model
    validates do
      format_of...
      presence_of...
      acceptance_of...
      confirmation_of...
      length_of...
      numericality_of...
      format_of...
      each...
    end
  end

You may also perform the usual 'longhand' way to assign default model validates directly within the model class itself:

  class MyModel < Sequel::Model
    validates_format_of...
    validates_presence_of...
    validates_acceptance_of...
    validates_confirmation_of...
    validates_length_of...
    validates_numericality_of...
    validates_format_of...
    validates_each...
  end
Something went wrong with that request. Please try again.