Skip to content

Instantly share code, notes, and snippets.

@mitsuhirookuno
Created June 11, 2020 03:26
Show Gist options
  • Save mitsuhirookuno/353f5dd4660565719ef7488877f9443f to your computer and use it in GitHub Desktop.
Save mitsuhirookuno/353f5dd4660565719ef7488877f9443f to your computer and use it in GitHub Desktop.
書きぶり検討
# frozen_string_literal: true
@media_website_id = 2_136
def before_labels
registered_labels = []
MediaWebsite.eager_load(media_website_labels: :label)
.where(id: @media_website_id).first
.media_website_labels.each do |media_website_label|
label = media_website_label.label
parent_label = media_website_label.label.parent
registered_labels.push(media_website_label_id: media_website_label.id,
id: label.id,
name: label.name,
parent_id: parent_label.try(:id),
parent_name: parent_label.try(:name) || '-')
end
registered_labels
end
def after_labels
media_website = MediaWebsite.eager_load(media_website_labels: :label).find(@media_website_id)
media_website.media_website_labels.map do |media_website_label|
label = media_website_label.label
registered_label = {
media_website_label_id: media_website_label.id,
id: label.id,
name: label.name,
parent_id: nil,
parent_name: '-'
}
next registered_label if label.root?
registered_label.merge!(
parent_id: label.parent.id,
parent_name: label.parent.name
)
end
end
def main
ap before_labels == after_labels
ap before_labels
ap after_labels
end
main
@mitsuhirookuno
Copy link
Author

  • 親の有無を root? で確認する
    • 親なら早期リターン
    • 子供なら親をマージでアップデート

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment