Last active
April 30, 2018 16:43
-
-
Save Startouf/d12a38d2be5bc3cae6462f4ba031c75f to your computer and use it in GitHub Desktop.
Small gist illustrating using has_one instead of belongs_to to take advantage of parent passing
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
class Moderation | |
belongs_to :content | |
end | |
class Content | |
has_many :moderations | |
end | |
class ModerationResource < ApplicationResource | |
type :'moderation' | |
model ::Moderation | |
# Note : use has_one despite having the belongs_to key to pass the moderation to the contentResource ! | |
has_one :content, resource: ::Moderation::ContentResource | |
end | |
class Moderation::Content < ApplicationResource | |
type :'moderation' | |
model ::Moderation | |
def update(attributes, moderation) | |
# If we had used a `belongs_to` in the resource, +moderation+ would be nil ! | |
ModerationService.new(moderation).update_content_attributes(attributes) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment