Skip to content

Instantly share code, notes, and snippets.

@nicholasjhenry
Created October 18, 2011 00:49
Show Gist options
  • Save nicholasjhenry/1294347 to your computer and use it in GitHub Desktop.
Save nicholasjhenry/1294347 to your computer and use it in GitHub Desktop.
Domain Model from SQL Antipatterns: Avoiding the Pitfalls of Database Programming
# SQL Antipatterns: http://pragprog.com/book/bksqla/sql-antipatterns
# A relationship between a model and a DAO like ActiveRecord should be HAS-A
# (aggregation) instead of (inheritance). Most frameworks that rely on
# ActiveRecord assume the IS-A solution. If you model uses DAOs instead of
# inheriting from the DAO class, then you can design the model to contain all
# data and code for the domain it's supposed to model--even if it takes
# multiple database tables to represent it.
# BugReport is a domain object encapsulating:
# * Bugs
# * Accounts
# * Products
class BugReport
class << self
def create(attributes={}, reported_by)
attributes.merge!(:status => "NEW", :reported_by => reported_by)
Bug.create(attributes)
end
def assign_user(bug_id, assign_to)
bug = Bug.find(bug_id)
bug.assigned_to = assigned_to
bug.save
end
def fetch(bug_id)
Rails.cache.fetch(:bugs, bug_id) do
Bug.find(bug_id)
end
end
def search(status, search_string)
Rails.cache.fetch(:bug_searches, status, search_string) do
Bug.includes(:product)
.where(:status => status)
.where("bugs.summary like '%?%' OR bugs.description like '%?%'",
search_string, search_string)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment