A simple plugin for Sequel::Model's that lets you easily create polymorphic associations.
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
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
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
Check the Advanced Associations section of the Sequel docs (search "Polymorphic Associations")