Regarding the event that the Ruby on Rails batch process cannot load the model.

Asked 2 years ago, Updated 2 years ago, 43 views

I'm thinking of implementing Ruby on Rails to run web APIs regularly, process data, and insert data into DB.
The model cannot be called due to an unknown cause error listed below.

lib/tasks/repository.rake

require'net/http'
require 'uri'

namespace —repository do
  desc "get repositories list and delete old repositories list at 11:00 every day"
  task —get_repositions do
    save_repositors
    delete_repositors
  end

  def save_repositors
    languages.each do | language |

      uri=
          URI.parse("https://api.github.com/search/repositories?q=language: #{language}&sort=stars&order=desc")
      https=Net::HTTP.new(uri.host,uri.port)
      https.use_ssl=true
      response = https.start {
        https.get(uri.request_uri)
      }

      if response.code == '200'
        results=ActiveSupport::JSON.decode response.body
        results["items"].each do | repository |
          Repository.create(name:repository["name"], language:repository["language"],
                            url: repository ["html_url"], scores: repository ["score"], issues: repository ["issues")
        end
      end
    end
  end

  def delete_repository
    puts "Hello World"
  end

  deflanguages
    ["Javascript", "Ruby", "Python", "Java", "Go"]
  end
end

app/models/repository.rb

class Repository <ApplicationRecord

end

Error Description:

rake aborted!
NameError:uninitialized constant repository

If you do not have enough information, please leave a comment.

ruby-on-rails ruby

2022-09-30 16:06

1 Answers

 task get_repositions::environment do
  hogehoge
end

As shown in , specifying environment as a dependent task seems to solve this problem.


2022-09-30 16:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.