Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Wesly Mezarina asked in General

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
Reply

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.

Reply

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

Reply

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.

Reply

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.

Reply

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.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,733+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.