ruby 小要点说明使用has_one而不是belongs_to来利用父传递

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 小要点说明使用has_one而不是belongs_to来利用父传递相关的知识,希望对你有一定的参考价值。

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

以上是关于ruby 小要点说明使用has_one而不是belongs_to来利用父传递的主要内容,如果未能解决你的问题,请参考以下文章