last update: 2009-05-01 04:55:14 GMT This book is a work in progress; feel free to contribute.

The Merb Open Source Book
Français | 한국어 | 日本語 | Português | Deutsch | 中文 | Español | Русский | Bosanski | Nederlands | العربية | Български | Italiano

Model Associations

Sequel::Model has a very simple set of association methods. Sequel provides many_to_one, one_to_many and many_to_many to create database associations.

One to Many

class Post < Sequel::Model
  one_to_many :comments
end

The Post class above now has a set of association methods similar to ActiveRecord’s has_many. The method is actually aliased to has_many, to maintain some ActiveRecord similarity.

Many to One

class Post < Sequel::Model
  many_to_one :user
end

The many_to_one association can also be called by belongs_to.

Many to Many

class Post < Sequel::Model
  many_to_many :tags
end

The many_to_many association can also be called by has_and_belongs_to_many.

One to One

Sequel::Model does not have a separate method to indicate a one_to_one relationship. A :one_to_one parameter can be passed to one_to_many to create this association.

class User < Sequel::Model
  one_to_many :addresses, :one_to_one => true
end