Skip to content

Instantly share code, notes, and snippets.

@inbeom
Created November 10, 2012 12:33
Show Gist options
  • Save inbeom/4050953 to your computer and use it in GitHub Desktop.
Save inbeom/4050953 to your computer and use it in GitHub Desktop.
A weird mongoid relation
module Abnormal
module Models
class Issue
include Mongoid::Document
field :exception_class, type: String
field :message, type: String
field :uniqueness_hash, type: String
field :status, type: String, default: OPEN
field :_id, type: String, default: -> { "#{app_id}#{uniqueness_hash}" }
has_many :errors, class_name: 'Abnormal::Models::Error', inverse_of: :issue
belongs_to :app, class_name: 'Abnormal::Models::App', inverse_of: :issues
end
end
end
module Abnormal
module Models
class Error
include Mongoid::Document
field :uniqueness_hash, type: String
embeds_one :exception, class_name: 'Abnormal::Models::Exception'
embeds_one :client, class_name: 'Abnormal::Models::Client'
embeds_one :application_environment, class_name: 'Abnormal::Models::ApplicationEnvironment'
embeds_one :request, class_name: 'Abnormal::Models::Request'
field :context, type: Hash
field :rescue_block, type: Hash
belongs_to :issue, class_name: 'Abnormal::Models::Issue', inverse_of: :errors
belongs_to :app, class_name: 'Abnormal::Models::App', inverse_of: :errors
after_create :find_or_create_issue
def find_or_create_issue
_issue = Models::Issue.find_or_create_with_error self
_issue.errors << self
_issue
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment