A simple plugin for Sequel that lets you easily create polymorphic associations.
Version 4.x is required.
Sequel::Model.plugin(:polymorphic)
class Asset < Sequel::Model
many_to_one :attachable, :polymorphic => true
end
class Note < Sequel::Model
one_to_many :assets, :as => :attachable
end
class Post < Sequel::Model
one_to_many :assets, :as => :attachable
many_to_one :postable, polymorphic: true
end
class Article < Sequel::Model
one_to_one :post, as: :postable
end
class Question < Sequel::Model
one_to_one :post, as: :postable
end
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
See specs.