require "sequel"

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

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

  author = Class.new Sequel::Model(db[:authors]) do
    def self.name; "author"; end
  end

  book = Class.new Sequel::Model(db[:books]) do
    def self.name; "book"; end
  end

  author.many_to_many :books, :class => book
  book.many_to_many :authors, :class => author

  puts author.create.books
end

db_scope Sequel.connect("postgres://user@localhost")

it works fine. But it is just a dirty workaround. I think we can implement 
some option like ":associated_class" that will be used instead of 
":class_name" constantization. For example:

author.many_to_many :books, :associated_class => book
book.many_to_many :authors, :associated_class => author

What do you think about it? Thank you.

-- 
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