Skip to content

Instantly share code, notes, and snippets.

@Startouf
Last active April 30, 2018 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Startouf/d12a38d2be5bc3cae6462f4ba031c75f to your computer and use it in GitHub Desktop.
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
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