DEV Community

Cover image for That was quite an episode...
JaredHarbison
JaredHarbison

Posted on • Edited on

2

That was quite an episode...

Rails + React + Redux - Pt 5


This post is going to focus on creating an Appearance for each queen. I'll iterate through each Episode's contestants' ids to find_or_create_by the contestants' drag_names then create the Appearance using the Episode and Queen ids.


Let's get started!


1. Def get_seasons in season.rb to scrape the list of season names from Fandom, concatenate each season name into an array of URLS for each Season's Wikipedia page, then iterate through the array to .create!() an instance of each Season.

__see previous posts for the gist__

2. Def get_queens in queen.rb to scrape the list of queens' names from Fandom, concatenate each queen's name into an array of URLs for each Queen's Fandom page, then iterate through the array to .create!() an instance of each Queen and her attributes (including associations for Quotes and Trivia.

__see previous posts for the gist__

3. With Seasons and Queens instantiated, iterate through the Seasons and .create!() an appearance for each episode per Queen and her appropriate appearance attributes.

class Appearance < ApplicationRecord
belongs_to :queen
belongs_to :episode
def get_appearances
#### iterate through each season, pulling episode ids and contestants
Season.all.each do |season|
season.episodes.each do |episode|
episode_id = episode.id
#### the stored array of contestants needs a little work before we create Appearances
contestants = episode.contestants.split(", ").map do |contestant|
contestant.gsub(/[^0-9a-z%&!\n\/(). ]/i, '').strip
end
#### iterate through the array of contestants to create an Appearance for each Episode
contestants.map do |contestant|
Appearance.create(
episode_id: episode_id,
#### use the contestants list as it appeared on the season's Fandom page to find the corresponding Queens
queen_id: Queen.find_or_create_by(drag_name: contestant).id
)
end
end
end
end
end
view raw appearance.rb hosted with ❤ by GitHub

A subsequent post will include the last in the scraping segment of the project, where I'll gather the queens stats from each episode and store them with her appearance! Then I'll have enough data to start creating the user interface.


That's all folks!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay