Skip to content

Instantly share code, notes, and snippets.

@cyx
Forked from myobie/example.rb
Created April 20, 2012 04:37
Show Gist options
  • Save cyx/2426038 to your computer and use it in GitHub Desktop.
Save cyx/2426038 to your computer and use it in GitHub Desktop.
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