{"id":849,"date":"2025-01-15T18:00:00","date_gmt":"2025-01-15T09:00:00","guid":{"rendered":"https:\/\/blog.fundely.co.jp\/tech\/?p=849"},"modified":"2025-01-14T14:12:16","modified_gmt":"2025-01-14T05:12:16","slug":"delegate%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e9%96%a2%e9%80%a3%e5%85%88%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e7%b0%a1%e6%bd%94%e3%81%ab%e5%8f%96%e5%be%97%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blog.fundely.co.jp\/tech\/2025\/01\/15\/delegate%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e9%96%a2%e9%80%a3%e5%85%88%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e7%b0%a1%e6%bd%94%e3%81%ab%e5%8f%96%e5%be%97%e3%81%99%e3%82%8b\/","title":{"rendered":"delegate\u3092\u4f7f\u3063\u3066\u95a2\u9023\u5148\u30c7\u30fc\u30bf\u3092\u7c21\u6f54\u306b\u53d6\u5f97\u3059\u308b"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.fundely.co.jp\/tech\/wp-content\/uploads\/sites\/2\/2025\/01\/\u540d\u79f0\u672a\u8a2d\u5b9a\u306e\u30c7\u30b6\u30a4\u30f3-2-1024x1024.png\" alt=\"\" class=\"wp-image-870\" srcset=\"https:\/\/blog.fundely.co.jp\/tech\/wp-content\/uploads\/sites\/2\/2025\/01\/\u540d\u79f0\u672a\u8a2d\u5b9a\u306e\u30c7\u30b6\u30a4\u30f3-2-1024x1024.png 1024w, https:\/\/blog.fundely.co.jp\/tech\/wp-content\/uploads\/sites\/2\/2025\/01\/\u540d\u79f0\u672a\u8a2d\u5b9a\u306e\u30c7\u30b6\u30a4\u30f3-2-300x300.png 300w, https:\/\/blog.fundely.co.jp\/tech\/wp-content\/uploads\/sites\/2\/2025\/01\/\u540d\u79f0\u672a\u8a2d\u5b9a\u306e\u30c7\u30b6\u30a4\u30f3-2-150x150.png 150w, https:\/\/blog.fundely.co.jp\/tech\/wp-content\/uploads\/sites\/2\/2025\/01\/\u540d\u79f0\u672a\u8a2d\u5b9a\u306e\u30c7\u30b6\u30a4\u30f3-2-768x768.png 768w, https:\/\/blog.fundely.co.jp\/tech\/wp-content\/uploads\/sites\/2\/2025\/01\/\u540d\u79f0\u672a\u8a2d\u5b9a\u306e\u30c7\u30b6\u30a4\u30f3-2.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u3053\u3093\u306b\u3061\u306f\u3002\u30c7\u30b6\u30a4\u30f3\u30fb\u30b7\u30b9\u30c6\u30e0\u5ba4\u306e\u6c38\u91ce\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4eca\u56de\u306f\u3001Rails \u306e delegate \u3092\u7528\u3044\u305f\u95a2\u9023\u5148\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u65b9\u6cd5\u306b\u3064\u3044\u3066\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u4ee5\u524d\u304b\u3089\u5b58\u5728\u81ea\u4f53\u306f\u8a8d\u8b58\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u3042\u307e\u308a\u4f7f\u7528\u3057\u305f\u3053\u3068\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u8a66\u3057\u306b\u4f7f\u7528\u3057\u3066\u307f\u308b\u3068\u3001\u610f\u5916\u306b\u4fbf\u5229\u306a\u3053\u3068\u306b\u6c17\u3065\u304d\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u3053\u308c\u304b\u3089delegate\u3092\u7528\u3044\u3066\u3001\u95a2\u9023\u5148\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u307f\u305f\u3044\u3068\u8003\u3048\u3066\u3044\u308b\u65b9\u306e\u53c2\u8003\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">delegate \u3068\u306f\uff1f\uff1f<\/h3>\n\n\n\n<p>delegate \u306f\u3001\u95a2\u9023\u3059\u308b\u30e2\u30c7\u30eb\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5c5e\u6027\u3084\u30e1\u30bd\u30c3\u30c9\u3092\u7c21\u6f54\u306b\u547c\u3073\u51fa\u305b\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306e\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3092\u5411\u4e0a\u3055\u305b\u3001\u5197\u9577\u306a\u8a18\u8ff0\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4f7f\u3044\u65b9\u306f\u4e0b\u8a18\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code># \u5bfe\u8c61\u306eUser\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u3072\u3082\u3065\u304fProfile\u30e2\u30c7\u30eb\u306e name\u30ab\u30e9\u30e0\u306e\u5024\u304c\u3001\u300c\u30c6\u30b9\u30c8\u592a\u90ce\u300d\u3068\u3059\u308b\n&gt; User.last.profile.name\n=&gt; &quot;\u30c6\u30b9\u30c8\u592a\u90ce&quot;\n\n# user.rb\nclass User &lt; ApplicationRecord\n  has_one :profile\n\n  def name\n    profile.name\n  end\nend\n\n# User\u30e2\u30c7\u30eb\u306b\u5b9a\u7fa9\u3057\u305fname\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3057\u3001name\u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\n&gt; User.last.name \n=&gt; &quot;\u30c6\u30b9\u30c8\u592a\u90ce&quot;\n\n# delegate \u3092\u4f7f\u7528\u3057\u3001profiles \u30c6\u30fc\u30d6\u30eb\u306ename\u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\nclass User &lt; ApplicationRecord\n  has_one :profile\n\n  delegate :name, to: :profile\nend\n\n&gt; User.last.name \n=&gt; &quot;\u30c6\u30b9\u30c8\u592a\u90ce&quot;<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<p>\u53c2\u8003<\/p>\n\n\n\n<p><a href=\"https:\/\/railsguides.jp\/active_support_core_extensions.html#%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E3%81%AE%E5%A7%94%E8%AD%B2\">Active Support \u30b3\u30a2\u62e1\u5f35\u6a5f\u80fd &#8211; Rails\u30ac\u30a4\u30c9<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">delegate\u3092\u7528\u3044\u305f\u95a2\u9023\u5148\u30c7\u30fc\u30bf\u306e\u53d6\u5f97<\/h3>\n\n\n\n<p>\u4eca\u56de\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30e2\u30c7\u30eb\u3067\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u304c\u7d44\u307e\u308c\u3066\u3044\u305f\u6642\u3001customer_coupons \u30c6\u30fc\u30d6\u30eb\u304b\u3089 customer_coupons &gt; serial_numbers &gt; items &gt; unifide_codes \u30c6\u30fc\u30d6\u30eb\u306e code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3057\u305f\u3044\u3068\u8003\u3048\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>class CustomerCoupon &lt; ApplicationRecord\n  belongs_to :serial_number, optional: true\nend\n\nclass SerialNumber &lt; ApplicationRecord\n  has_one :customer_coupon\nend\n\nclass Item &lt; ApplicationRecord\n  belongs_to :unified_code, optional: true\n  has_many :serial_numbers\nend\n\nclass UnifiedCode &lt; ApplicationRecord\n  has_one :item, dependent: :destroy\nend<\/code><\/pre><\/div>\n\n\n\n<p>\u305d\u3053\u3067\u3001\u5f53\u521d\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u304b\u305f\u3061\u3067code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3057\u3088\u3046\u3068\u8003\u3048\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code># code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\n&gt; CustomerCoupon.last.serial_number.item.unified_code.code\n\n=&gt; &quot;2005181101&quot;<\/code><\/pre><\/div>\n\n\n\n<p>\u305f\u3060\u3001\u3053\u308c\u3060\u3068\u3001customer_coupons \u30c6\u30fc\u30d6\u30eb\u306b\u3072\u3082\u3065\u304funified_codes \u30c6\u30fc\u30d6\u30eb\u306e code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3057\u305f\u3044\u3068\u306a\u3063\u305f\u6642\u3001\u6bce\u56de\u3053\u306e\u3088\u3046\u306a\u8a18\u8ff0\u3092\u3059\u308b\u306e\u306f\u5c11\u3057\u5197\u9577\u306a\u6c17\u304c\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001customer_coupons \u30c6\u30fc\u30d6\u30eb\u306b\u3072\u3082\u3065\u304funified_codes \u30c6\u30fc\u30d6\u30eb\u306e code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u3060\u3051\u306b\u3001CustomerCoupon\u30e2\u30c7\u30eb\u306b unified_codes \u30c6\u30fc\u30d6\u30eb\u306e code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u6210\u3059\u308b\u306e\u3082\u3001\u5c11\u3057\u9055\u3046\u6c17\u304c\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u305d\u3053\u3067\u3001delegate \u3092\u4f7f\u7528\u3057\u3001\u3088\u308a\u7c21\u5358\u306bcode\u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5b9f\u88c5\u3057\u305f\u5185\u5bb9\u304c\u4e0b\u8a18\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>class CustomerCoupon &lt; ApplicationRecord\n  belongs_to :serial_number, optional: true\n  \n  delegate :item, to: :serial_number\n  delegate :code, to: :item, prefix: true\nend\n\nclass Item &lt; ApplicationRecord\n  belongs_to :unified_code, optional: true\n  has_many :serial_numbers\n  \n  delegate :code, to: :unified_code\nend<\/code><\/pre><\/div>\n\n\n\n<p>\u307e\u305a\u3001\u884c\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u3001CustomerCoupon \u30e2\u30c7\u30eb\u3067\u3001serial_number \u7d4c\u7531\u3067\u3001item\u3092\u53d6\u5f97\u3057\u3001CustomerCoupon.last.serial_number.item \u3067 item \u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001Item\u30e2\u30c7\u30eb\u3067\u3001unified_code \u7d4c\u7531\u3067\u3001code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u3057\u3066\u3001CustomerCoupon \u30e2\u30c7\u30eb\u3067\u3001item \u7d4c\u7531\u3067\u3001code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001prefix \u3092\u7528\u3044\u308b\u3053\u3068\u3067\u3001CustomerCoupon.last.item_code \u3068\u3044\u3063\u305f\u304b\u305f\u3061\u3067\u3001customer_coupons \u30c6\u30fc\u30d6\u30eb\u304b\u3089 unified_codes \u30c6\u30fc\u30d6\u30eb\u306e code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u76f4\u63a5\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>CustomerCoupon.last.item_code \u306e\u5b9f\u884c\u7d50\u679c\u304c\u4e0b\u8a18\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>&gt; CustomerCoupon.last.item_code\n\n=&gt; &quot;2005181101&quot;<\/code><\/pre><\/div>\n\n\n\n<p>\u4e0a\u8a18\u3088\u308a\u3001delegate \u3092\u7528\u3044\u3066\u3001customer_coupons \u30c6\u30fc\u30d6\u30eb\u304b\u3089 unified_codes \u30c6\u30fc\u30d6\u30eb\u306e code \u30ab\u30e9\u30e0\u306e\u5024\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h3>\n\n\n\n<p>\u4eca\u56de\u306f\u3001\u95a2\u9023\u5148\u306e\u30c7\u30fc\u30bf\u3092delegate \u3092\u7528\u3044\u3066\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u500b\u4eba\u7684\u306b\u306f\u3001\u3068\u3066\u3082\u4fbf\u5229\u306a\u6a5f\u80fd\u3067\u3042\u308b\u3068\u601d\u3044\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001delegate \u81ea\u4f53\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066\u6271\u308f\u308c\u308b\u306e\u3067\u3001render json: coupon.as_json(methods: [:serial, :item_code]) \u306e\u3088\u3046\u306b\u3001JSON\u5f62\u5f0f\u3067\u5b9f\u884c\u7d50\u679c\u3092\u30d5\u30ed\u30f3\u30c8\u5074\u306b\u8fd4\u3059\u3053\u3068\u3082\u53ef\u80fd\u3089\u3057\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4eca\u5f8c\u3082 delegate \u3092\u9069\u5207\u306a\u304b\u305f\u3061\u3067\u4f7f\u7528\u3057\u3066\u3044\u304d\u305f\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u73fe\u5728\u30c7\u30b6\u30a4\u30f3\u30fb\u30b7\u30b9\u30c6\u30e0\u5ba4\u3067\u306f\u3001\u65b0\u3057\u3044\u30e1\u30f3\u30d0\u30fc\u3092\u52df\u96c6\u3057\u3066\u3044\u307e\u3059\u3002<br> \u5c11\u3057\u3067\u3082\u8208\u5473\u3092\u6301\u305f\u308c\u305f\u65b9\u306f\u3001\u305c\u3072\u3054\u5fdc\u52df\u304f\u3060\u3055\u3044\u3002<br>\u7686\u69d8\u304b\u3089\u306e\u3054\u5fdc\u52df\u3001\u5fc3\u3088\u308a\u304a\u5f85\u3061\u3057\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button aligncenter is-style-fill\"><a class=\"wp-block-button__link has-vivid-green-cyan-background-color has-background wp-element-button\" href=\"https:\/\/blog.fundely.co.jp\/tech\/?page_id=82&amp;preview=true\" style=\"border-radius:12px\">\u52df\u96c6\u8981\u9805\u304a\u3088\u3073\u30a8\u30f3\u30c8\u30ea\u30fc\u30d5\u30a9\u30fc\u30e0<\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u30c7\u30b6\u30a4\u30f3\u30fb\u30b7\u30b9\u30c6\u30e0\u5ba4\u306e\u6c38\u91ce\u3067\u3059\u3002 \u4eca\u56de\u306f\u3001Rails \u306e delegate \u3092\u7528\u3044\u305f\u95a2\u9023\u5148\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u65b9\u6cd5\u306b\u3064\u3044\u3066\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002 \u4ee5\u524d\u304b\u3089\u5b58\u5728\u81ea\u4f53\u306f\u8a8d\u8b58\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u3042\u307e\u308a\u4f7f\u7528\u3057\u305f\u3053\u3068\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u3001 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,17],"tags":[],"class_list":["post-849","post","type-post","status-publish","format-standard","hentry","category-rails","category-ruby"],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/posts\/849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/comments?post=849"}],"version-history":[{"count":16,"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/posts\/849\/revisions"}],"predecessor-version":[{"id":945,"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/posts\/849\/revisions\/945"}],"wp:attachment":[{"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/media?parent=849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/categories?post=849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fundely.co.jp\/tech\/wp-json\/wp\/v2\/tags?post=849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}