Skip to main content

How do i update an attribute for belongs_to association when scraping a site?

General • Asked by Wesly Mezarina

So pretty much a anime belongs_to :season and a season has_many :animes.
I'm scraping a site and i'm collecting the season like "Spring 2015". I want to be able to assign "Spring 2015" to a anime so it can go to the collection of "Spring 2015". Is there a way to doing this? I was thinking of doing anime.update_attribute(:season_id, season) but that wont work.

My .rake file

desc "Fetch anime info"
task :fetch_anime_info => :environment do
 require "open-uri"
 require 'mechanize'

 Anime.all.each do |anime|
  season_scrape = doc.css('div#content .borderClass .js-scrollfix-bottom div:contains("Premiered")').text.split(' ')[1..-1]
  if season_scrape.blank?
   season = ""
  else
   season = season_scrape.join(' ')
  end

  anime.update_attribute(:season,  season)
 end
end

Anime model

class Anime < ActiveRecord::Base
 belongs_to :season
end

Season model

class Season < ActiveRecord::Base
 has_many :animes
end

So i figured out that i can use anime.update(:season_attributes => {:title => season}) to update it but i get a weird url.
I get something like "spring-2016-fba9c08e-458e-4b50-93e4-ca41a522ed67". It might be because i already have Spring 2016 in the DB.
I'll keep updating.


anime.season.update(title: "Spring 2015")


Updating the season through the association works well.

And a side note: That url with the UUID at the end is what happens when friendly_id already detects a duplicate.


Do you guys know of another possible solution? I tried Drazen's code but i kept getting nil class error. With the code i provided it just keeps making a new season and only assigned one anime to the season.


Something like this should fix it:

desc "Fetch anime info"
task :fetch_anime_info => :environment do
 require "open-uri"
 require 'mechanize'

 Anime.all.each do |anime|
  season_scrape = doc.css('div#content .borderClass .js-scrollfix-bottom div:contains("Premiered")').text.split(' ')[1..-1]
  season_text = season_scrape.blank? ? "" : season_scrape.join(' ')

  season = Season.where(name: season_text).first_or_create
  anime.update(season: season)
 end
end

This way you'll look up the season to see if it exists, otherwise create a new one, and then you'll associate the anime to the season.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.