I can use the following workaround:

models = Module.new

author = Class.new Sequel::Model(db[:authors]) do
  many_to_many :books, :class_namespace => models, :class => :Book
end
models.const_set :Author, author

book = Class.new Sequel::Model(db[:books]) do
  many_to_many :authors, :class_namespace => models, :class => :Author
end
models.const_set :Book, book

But I've received the following error:
`constantize': "::#<Module:0x0000563626b9dce8>::Book" is not a valid 
constant name!

So it won't work.

def associated_class
  cached_fetch(:class) do
    begin
      constantize(self[:class_name])
    rescue NameError => e
      raise NameError, "#{e.message} (this happened when attempting to find 
the associated class for #{inspect})", e.backtrace
    end
  end
end

So maybe I need to override this method in order to use raw class model 
instead of constantizing global class name.


пятница, 9 февраля 2018 г., 12:13:17 UTC+3 пользователь Андрей Аладьев 
написал:
>
> Hello. I see that it is almost possible for sequel to work with multiple 
> databases. But I've found a design issue.
>
> require "sequel"
>
>
> def db_scope(db)
>   unless db.table_exists?(:authors)
>     db.create_table :authors do
>       primary_key :id
>     end
>   end
>
>
>   unless db.table_exists?(:books)
>     db.create_table :books do
>       primary_key :id
>     end
>   end
>
>
>   unless db.table_exists?(:authors_books)
>     db.create_table :authors_books do
>       primary_key :id
>       foreign_key :author_id, :authors
>       foreign_key :book_id, :books
>     end
>   end
>
>
>   authors = Class.new Sequel::Model(db[:authors]) do
>     many_to_many :books
>   end
>
>
>   books = Class.new Sequel::Model(db[:books]) do
>     many_to_many :authors
>   end
>
>
>   authors.create.books
> end
>
>
> db_scope Sequel.connect("postgres://user@localhost")
>
> We can see the following error:
>
> uninitialized constant Book (this happened when attempting to find the 
> associated class for 
> #<Sequel::Model::Associations::ManyToManyAssociationReflection 
> #<Class:0x00005654163d1fb0>.many_to_many :books>)
>
> Sequel wants "::Book" class. But it is not possible to provide such class 
> in global scope if we want to maintain multiple Books classes for multiple 
> db client instances. Book could be defined in db scope, not in global 
> scope. How to solve such question? Should I create a github issue?
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to