Skip to content

Instantly share code, notes, and snippets.

@shingara
Created November 23, 2012 13:34
Show Gist options
  • Save shingara/4135650 to your computer and use it in GitHub Desktop.
Save shingara/4135650 to your computer and use it in GitHub Desktop.
## Model
class User
include DataMapper::Resource
property :id, Serial
property :first_name, String
property :last_name, String
end
## How manage his query interface. Doing directly in User class and combine them ex :
#
class User
include DataMapper::Resource
property :id, Serial
property :first_name, String
property :last_name, String
def self.with_first_name(name)
all({:first_name => name})
end
def self.with_last_name(name)
all({:last_name => name})
end
end
User.with_first_name(name).with_last_name(name)
## Or create a new class to manage all Query stuff like :
class UserRequest
def self.with_first_name(name)
User.all({:first_name => name})
end
def self.with_last_name(name)
User.all({:last_name => name})
end
end
UserRequest.with_first_name(name).with_last_name(name)
## The second solution avoid put method in your model and separate your Query to your model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment