sendgrid / sendgrid-ruby

The Official Twilio SendGrid Led, Community Driven Ruby API Library
https://sendgrid.com
MIT License
621 stars 324 forks source link

TypeError: superclass mismatch for class Client #439

Closed timpwbaker closed 3 years ago

timpwbaker commented 3 years ago

Issue Summary

I have an ActiveRecord model Client and a rake task that includes sendgrid.

Within the rake db:seed task I cannot access Client, trying to interact with the Client class in any way results in "TypeError: superclass mismatch for class Client"

Steps to Reproduce

  1. rails new client_issue --database=postgresql -T
  2. cd client_issue
  3. echo 'gem "sendgrid-ruby"' >> Gemfile
  4. bundle
  5. rails g scaffold client name:string
  6. rake db:create
  7. rake db:migrate
  8. touch lib/tasks/task.rake
  9. echo 'require "sendgrid-ruby"' >> lib/tasks/task.rake
  10. echo 'include SendGrid' >> lib/tasks/task.rake
  11. echo 'Client.create(name: "foo")' >> db/seeds.rb
  12. rake db:seed

Exception/Log

rake aborted!
TypeError: superclass mismatch for class Client
/Users/timothybaker/projects/hexarad/client_issue/app/models/client.rb:1:in `<main>'
/Users/timothybaker/projects/hexarad/client_issue/db/seeds.rb:8:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

Technical details:

timpwbaker commented 3 years ago

I found a solution to my exact issue.

The clash is avoided if I move the include into the task action block:

namespace :important_task do
  desc "Does some important thing"
  task :action => :environment do
    require 'sendgrid-ruby'
    include SendGrid

    ImportantThingWorker.perform
  end
end

It would still be a problem however if I wanted to access the Client model inside the rake task:

namespace :assignments_email do
  desc "Sends the assignments backup"
  task :send => :environment do
    require 'sendgrid-ruby'
    include SendGrid

    Client.create(name:"foo")
  end
end

rake assignments_email:send # => TypeError: superclass mismatch for class Client