Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 1.19 KB

README.md

File metadata and controls

60 lines (45 loc) · 1.19 KB

Sequel Polymorphic

A simple plugin for Sequel::Model's that lets you easily create polymorphic associations.

Usage

Models

Sequel::Model.plugin(:polymorphic)

class Asset < Sequel::Model
  belongs_to :attachable, :polymorphic => true
end

class Note < Sequel::Model
  has_many :assets, :as => :attachable
end

class Post < Sequel::Model
  has_many :assets, :as => :attachable
end

Schema

Include the polymorphic columns in your DB schema:

Sequel.migration do
  change do
    create_table :assets do
      # ...
      Integer :attachable_id
      String :attachable_type
      # ...
      index [:attachable_id, :attachable_type]
    end
  end
end

Similar to ActiveRecord Style

class Asset < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true
end

class Post < ActiveRecord::Base
  has_many :assets, :as => :attachable
end

class Note < ActiveRecord::Base
  has_many :assets, :as => :attachable
end

Sequel (without the polymorphic plugin)

Check the Advanced Associations section of the Sequel docs (search "Polymorphic Associations")