0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

30分記事<2025/02/26> ポリモーフィックデー!!!

Posted at

1.はじめに

・この記事は記載者の自己満足による物です。
・30分で完全に切り上げるために、完全に途中で切れても終わります!笑
(このはじめにを書くのは毎回テンプレとし、このコピペの時間帯は時間に入れないものとする。)
・間違えている可能性あり!!!あまりこの記事信用しないほうがいい気がする!笑

2.なんで始めようと思ったか?

・「時間がないから記事が書けない」を言い訳に書いていなかった自分を律するため
・「意味もない記事でもいいから30分で書いて、書く練習をしてみよう!そうすりゃアウトプットにもなる」と思ったため。

内容がなくともとにかく書こう。

・いいんですよ。中身すっからかんでも。練習になりゃぁ。

ってことで2025/2/19からスタート!!

30分アウトプット開始

ちょっと空いちゃった。30分記事以外に題名つけようかな。
夜中遅くまで勉強していて「明日やろう」とするとやっぱりできなくなってしまう。
夜1時だけどかいていこー!!
(けど進展がなかったりするとやっぱり書きづらいっていうのがあったり。。。)

今日はポリモーフィックデー!!
٩( ᐛ )وファ???と思うところが沢山あったけど、ひとまず復習がてら書いていこうと思う。

ひとまずポリモーフィックというのは…
一つの物体(記事)に対して、複数の物を紐づけようとする行為。。。といえばいいのかな。
まーーーじでわかり辛え(● ˃̶͈̀ロ˂̶͈́)੭ꠥ⁾⁾ww

モデルを書くと

class Article < ApplicationRecord
  has_many :article_blocks, -> { order(:level) }, inverse_of: :article
  has_many :sentences, through: :article_blocks, source: :blockable, source_type: 'Sentence'
  has_many :media, through: :article_blocks, source: :blockable, source_type: 'Medium'
  has_many :embeds, through: :article_blocks, source: :blockable, source_type: 'Embed'
  • 「Article」1:多「sentences」、「media」、「embeds」で関係があって、
  • 中間テーブルとして「article_blocks」がある。
  • 「source: :blockable」→blockableというテーブルが先がないけど、
     「article_blocks」テーブルの中に「blockable_id」「blockable_type」というカラムがつけられる。

  • 「blockable」テーブル自体は存在しないが、「article_blocks」という中に存在している「blockable_id」「blockable_type」によって仮想上のテーブルが作成されている?

…言っている事意味不明と言われても仕方ないじゃん。
テーブル検索しても無いし、ましてや以下モデルの「belongs_to :blockable」ってどこさしてんのよ!?w

class ArticleBlock < ApplicationRecord
  belongs_to :article
  belongs_to :blockable, polymorphic: true, dependent: :destroy

・「Article_id」:「Blockable_id」で中間テーブルの多:多を表していて
 さらにそこに「Blockable_id」:「Blockable_type」 にする事で、多:多の多を表している??

image.png

ここで意味わからんようにしているのが

Sentences,Media,Embedsのモデルの存在。
こいつらのModelを見てみると

・class Sentence < ApplicationRecord
  has_one :article_block, as: :blockable, dependent: :destroy
  has_one :article, through: :article_block
end

・class Medium < ApplicationRecord
  has_one :article_block, as: :blockable, dependent: :destroy
  has_one :article, through: :article_block

  has_one_attached :attachment

  enum media_type: { image: 0 }

  validates :attachment, attachment: { purge: true, content_type: %r{\Aimage/(png|jpeg)\Z}, maximum: 10_485_760 }, if: :image?
end

#

・class Embed < ApplicationRecord
  has_one :article_block, as: :blockable, dependent: :destroy
  has_one :article, through: :article_block

  enum embed_type: { youtube: 0 }

  validates :identifier, length: { maximum: 200 }
end

・・・全部「has_one :article」。。。だと???
articleでは「has_many」なのにどういうこっちゃワレェ!!!ww

かつ、頭がこんがらがってるから、

多:多のテーブルの場合だったら、(まあhas_oneだから違うけど。)
「Blockable_type」に「sentences」、「media」、「embeds」が入り
「Blockable_id」に上記3つのテーブルの情報がid=1からそれぞれ入る。と思いきや。

まず、「sentences」を3つ入れる
 「sentences」のid → 1−3
「Blockable_type」→Sentence
「Blockable_id」→1−3

次に「embeds」を1つ入れるs
「embeds」のid → 1
「Blockable_type」→ Embed
「Blockable_id」 → 4

٩( ᐛ )وファッッッ?????

まーーーじでどうゆう事っすかw
ひとまず。記事も出てないから人気ないという認識でいいのだろうか…笑

勉強は終わらないっっ\\\٩( 'ω' )و ////w

本日は1分半残ってるとこだけど終了!
ではまたっ(● ˃̶͈̀ロ˂̶͈́)੭ꠥ⁾⁾!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?