Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Created October 3, 2011 06:23
Show Gist options
  • Save d11wtq/1258550 to your computer and use it in GitHub Desktop.
Save d11wtq/1258550 to your computer and use it in GitHub Desktop.
class Message
include DataMapper::Resource
property :id, Serial
property :deleted, Boolean
belongs_to :sender,
'User'
belongs_to :recipient,
'User'
class << self
def visible
all(:deleted => false)
end
end
end
class User
include DataMapper::Resource
property :id, Serial
def messages
Message.all(:sender => self) | Message.all(:recipient => self)
end
end
# this works (returns records from the current query that have :deleted => false)
user = User.get(1)
user.messages.all(:deleted => false).count
# this doesn't (returns *all* records with :deleted => false... not scoped to the current query)
user.messages.visible.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment