Created
May 19, 2017 15:52
-
-
Save AJFaraday/59ae57d200998e744995eeaf6989ed8d to your computer and use it in GitHub Desktop.
conundrum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How can this line throw an error? | |
@folder = object.respond_to?(:project_element) ? object.project_element : nil | |
# The error is this: | |
# undefined method `project_element' for #<Report::Event:0x007fbb6eb8ca78> | |
# Surely This should be protected, right? | |
# ----------------------------- | |
# Here's the answer. `object` is an instance of Report::Template, which contains this line. | |
module Report | |
class Template < ActiveRecord::Base | |
belongs_to :event, foreign_key: 'report_event_id' | |
delegate :project_element, :class_name, to: :event, allow_nil: true | |
#... | |
end | |
end | |
# but Report::Event doesn't implement `project_element` | |
# Report::Template responds to `project_element` by delegating it to the related Report::Event | |
# Report::Event does not respond to `project_element` so it throws an error. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment