Skip to content

Instantly share code, notes, and snippets.

@saleandro
Created June 12, 2012 15:50
Show Gist options
  • Select an option

  • Save saleandro/2918335 to your computer and use it in GitHub Desktop.

Select an option

Save saleandro/2918335 to your computer and use it in GitHub Desktop.
Describing how I use ActiveRecord models as Data Access objects.
module Models::Storage
class Artist < ActiveRecord::Base
validates_presence_of :name
end
end
---
module Models
class Artist
def self.search(query)
stored = storage.find_by_name(query)
new(stored)
end
def self.allowed_attributes
['name']
end
def initialize(stored)
@stored = stored
end
def update(data)
data = data.reject {|k, v| !self.class.allowed_attributes.include?(k.to_s) }
return true if data.empty?
@stored.update_attributes(data)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment