这种情况下的解决方案自动化程度较低:
require "ohm"class Book < Ohm::Model attr_accessor :authors attribute :title index :authorsendclass Author < Ohm::Model attribute :nameend###require "test/unit"class BooksTest < Test::Unit::TestCase def test_books_by_author dave = Author.create(name: "Dave") andy = Author.create(name: "Andy") dhh = Author.create(name: "DHH") pickaxe = Book.create(title: "Pickaxe", authors: [dave.id, andy.id]) assert_equal pickaxe, Book.find(authors: dave.id).first assert_equal pickaxe, Book.find(authors: andy.id).first assert_equal nil, Book.find(authors: dhh.id).first endend
说得通?



