Skip to content

Instantly share code, notes, and snippets.

@sipple
Last active August 29, 2015 14:07
Show Gist options
  • Save sipple/5ca9d4da46969527d950 to your computer and use it in GitHub Desktop.
Save sipple/5ca9d4da46969527d950 to your computer and use it in GitHub Desktop.
class ErrorInstance
def self.create_from(email)
#your other create_from code
#ActiveRecord block initialization
error_instance = ErrorInstance.new do |instance|
instance.error_id = Error.find_or_create(exception).id
instance.exception = exception
#etc, your other parameters go here
end
error_instance.save
# I'm not certain this needs to be returned. It might not, depending on how
# you end up implementing search.
return error_intsance
end
end
class Error
def most_recent_instance
ErrorInstance.order("timestamp").find_by(error_id: this.id)
end
def self.find_or_create(exception)
# try to find an error
error = Error.first(exception: exception)
# if we fail, create a new error
# It's possible Rails 4 ActiveRecord doesn't return 'nil' if no
# record is found on first. If not, this conditional will need
# to be changed a bit.
unless error do
#ActiveRecord hash initilization to create a new Error
error = Error.new(name: exception)
error.save
end
#return the error
error
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment