It's kind of hard to get along with, so after a while of studying, it becomes polymorphic ...?, So I enjoyed implementing it. It feels like a memorandum, so please take a look.
Implementing which Sakamichi Group is singing which song without defining polymorphic relations ...
nogizaka.rb
class Nogizaka < ApplicationRecord
has_many :songs
end
keyakizaka.rb
class Keyakizaka < ApplicationRecord
has_many :songs
end
hinatazaka.rb
class Hinatakizaka < ApplicationRecord
has_many :songs
end
song.rb
class Song < ApplicationRecord
belongs_to :nogizaka
belongs_to :keyakizaka
belongs_to :hinatazaka
end
――If you want to get information about idols who sing a certain song, you have to check which idol is singing. (If you're a fan of slopes, you'll understand without checking ... lol)
python
@song = Song.take #Get an instance appropriately
@song.nogizaka #In the case of Nogizaka46 songs
@song.hinatazaka #In the case of Hinatazaka46 songs
--46 When the number of groups increases, it is necessary to add a column to the song model.
--Just set polymorphic: true
to references
20200620xxxx_create_songs.rb
class CreateSongs < ActiveRecord::Migration[5.2]
def change
create_table :songs do |t|
t.string :name
t.references :songable, polymorphic: true, index: true #Here! !!
t.timestamps
end
end
end
schema.rb
create_table "songs", force: :cascade do |t|
t.string "name"
t.string "postable_type" #The type distinguishes between Nogizaka class and Keyakizaka class.
t.integer "postable_id"
...(The following is omitted)
end
I will rewrite the models as well. It's surprisingly easy.
class Nogizaka < ApplicationRecord
has_many :songs, as: :songable
end
class Keyakizaka < ApplicationRecord
has_many :songs, as: :songable
end
class Hinatakizaka < ApplicationRecord
has_many :songs, as: :songable
end
class Song < ApplicationRecord
belongs_to :songable, polymorphic: true
# Before ↓↓↓
# belongs_to :nogizaka
# belongs_to :keyakizaka
# belongs_to :hinatazaka
#3 lines are now 1 line!
end
# @When song is Nogizaka's song
#Normal association
@song.nogizaka # =>name:"Nogizaka46", year_of_formation:2011,...
#Polymorphic related
@song.songable # =>name:"Nogizaka46", year_of_formation:2011,...
--In the above, you can get an instance of the same Nogizaka model. ――I would be happy if you could get an image of it somehow.
Define a method with the same name in each model and change the behavior in it.
class Nogizaka < ApplicationRecord
has_many :songs, as: :songable
def cute_member
"name:#{name},Pets:#{pet_name}"
end
end
class Keyakizaka < ApplicationRecord
has_many :songs, as: :songable
def cute_member
"name:#{name},Birthplace:#{birthplace}"
end
end
class Hinatakizaka < ApplicationRecord
has_many :songs, as: :songable
def cute_member
"name:#{name},catch copy:#{catch_copy}"
end
end
python
#@When the song is Nogizaka
@song.songable.cute_member
=> "name:Yoda Yuki,Pets:Gonzo"
#@When the song is Keyakizaka
@song.songable.cute_member
=> "name:Hikaru Morita,Birthplace:Fukuoka Prefecture"
#@When the song is Hinatazaka
@song.songable.cute_member
=> "name:Hinano Kamimura,catch copy:Hinano!"
--You can get an instance of Nogizaka model (Keyakizaka, Hinatazaka) with @ song.songable
. (It's hard to get an image, and I think it's around here that makes polymorphic difficult.)
――Idols are cute after all. .. ..
Recommended Posts