Search code examples
rubysequelsequel-gem

Custom Table Mapping with Sequel gem


Been spinning my wheels. How would one specify a custom table mapping with the sequel gem?

I've done the following

init.rb

OTHER_DB = Sequel.connect(:adapter => 'tinytds', :host => 'host1', :database => 'mydatabase', :user => 'myuser', :password => 'mypassword')

Namespace::MyModel.db = OTHER_DB

MyModel.rb

module Namespace
  class MyModel < Sequel::Model('myschema.MyModelTable')
  end
end

It complains about the 'myschema.MyModelTable' in the constructor. I've also tried set_dataset('myschema.MyModelTable') with no success.

The docs seem to be a bit opaque on how to do this


Solution

  • I solved this with the following code

    module Namespace
      class MyModel < Sequel::Model(Sequel.qualify(:myschema,:MyModelTable))
      end
    end
    

    More examples can be found in the tests here