Skip to content

Instantly share code, notes, and snippets.

@ckdake
Created June 20, 2011 15:22
Show Gist options
  • Save ckdake/1035814 to your computer and use it in GitHub Desktop.
Save ckdake/1035814 to your computer and use it in GitHub Desktop.
make find_or_create_by methods work right with validations
# This and the next method are unfortunately 'magic'
# They set default paramaters for these two find_or_create_by_ method_missing methods
def self.find_or_create_by_email(params)
params = { email: params } unless params.is_a?(Hash)
super(params.reverse_merge(
password: Digest::MD5.hexdigest("#{rand(1024)}Time.now"),
username: Digest::MD5.hexdigest("#{rand(1024)}Time.now")
))
end
def self.find_or_create_by_username(params)
params = { username: params } unless params.is_a?(Hash)
super(params.reverse_merge(
email: Digest::MD5.hexdigest("#{rand(1024)}Time.now") + "@example.com",
password: Digest::MD5.hexdigest("#{rand(1024)}Time.now")
))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment