-
-
Save cyx/2426038 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book < Ohm::Model | |
collection :authors, :Author | |
end | |
class Author < Ohm::Model | |
reference :book, :Book | |
attribute :name | |
attribute :mood | |
index :mood | |
end | |
b = Book.create :id => 1 | |
Author.create :name => "Tom", :mood => "happy", :book => b | |
Author.create :name => "Dick", :mood => "angry", :book => b | |
Author.create :name => "Harry", :mood => "sad", :book => b | |
Author.create :name => "Jeff", :mood => "melancholy", :book => b | |
Author.create :name => "Jerry", :mood => "sad", :book => Book.create(:id => 2) # other book | |
Book[1].authors.size # => 4 | |
Book[1].authors.find(:mood => "sad").size # => 1 | |
Book[1].authors.find(:mood => "happy").find(:mood => "sad").size # => 0, so find means AND | |
Book[1].authors.find(:mood => "happy").union(:mood => "sad").size # => 3, so it found the author that isn't on book 1 because union just includes all sad people, not all sad people for book 1 | |
# What I really want is a way to find both happy and sad authors for book 1 only... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment