Skip to content

Commit

Permalink
Define the stub in the migration even if they're only used in the test
Browse files Browse the repository at this point in the history
Rails 7 can't find the associations when running the tests.  Resolves the test error:

Rails couldn't find a valid model for AddMissingEmsIdToSwitch::HostSwitch association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass.
  • Loading branch information
jrafanie committed May 22, 2024
1 parent 7d967f3 commit 51e448d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
28 changes: 28 additions & 0 deletions db/migrate/20190201173316_add_missing_ems_id_to_switch.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
class AddMissingEmsIdToSwitch < ActiveRecord::Migration[5.0]
class ExtManagementSystem < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

class Host < ActiveRecord::Base
self.inheritance_column = :_type_disabled

belongs_to :ext_management_system, :foreign_key => "ems_id", :class_name => "AddMissingEmsIdToSwitch::ExtManagementSystem"

has_many :host_switches, :class_name => "AddMissingEmsIdToSwitch::HostSwitch"
has_many :switches, :through => :host_switches, :class_name => "AddMissingEmsIdToSwitch::Switch"
end

class HostSwitch < ActiveRecord::Base
belongs_to :host, :class_name => "AddMissingEmsIdToSwitch::Host"
belongs_to :switch, :class_name => "AddMissingEmsIdToSwitch::Switch"
end

class Switch < ActiveRecord::Base
self.inheritance_column = :_type_disabled

belongs_to :ext_management_system, :foreign_key => "ems_id", :class_name => "AddMissingEmsIdToSwitch::ExtManagementSystem"
belongs_to :host, :class_name => "AddMissingEmsIdToSwitch::Host"

has_many :hosts, :through => :host_switches, :class_name => "AddMissingEmsIdToSwitch::Host"
has_many :host_switches, :class_name => "AddMissingEmsIdToSwitch::HostSwitch"
end

def up
say_with_time("Add missing ems_id to switch") do
connection.execute <<-SQL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
require_migration

class AddMissingEmsIdToSwitch < ActiveRecord::Migration[5.0]
class ExtManagementSystem < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

class Host < ActiveRecord::Base
self.inheritance_column = :_type_disabled

belongs_to :ext_management_system, :foreign_key => "ems_id", :class_name => "AddMissingEmsIdToSwitch::ExtManagementSystem"

has_many :host_switches, :class_name => "AddMissingEmsIdToSwitch::HostSwitch"
has_many :switches, :through => :host_switches, :class_name => "AddMissingEmsIdToSwitch::Switch"
end

class HostSwitch < ActiveRecord::Base
belongs_to :host, :class_name => "AddMissingEmsIdToSwitch::Host"
belongs_to :switch, :class_name => "AddMissingEmsIdToSwitch::Switch"
end

class Switch < ActiveRecord::Base
self.inheritance_column = :_type_disabled

belongs_to :ext_management_system, :foreign_key => "ems_id", :class_name => "AddMissingEmsIdToSwitch::ExtManagementSystem"
belongs_to :host, :class_name => "AddMissingEmsIdToSwitch::Host"

has_many :hosts, :through => :host_switches, :class_name => "AddMissingEmsIdToSwitch::Host"
has_many :host_switches, :class_name => "AddMissingEmsIdToSwitch::HostSwitch"
end
end

describe AddMissingEmsIdToSwitch do
let(:switch_stub) { migration_stub(:Switch) }
let(:host_stub) { migration_stub(:Host) }
Expand Down

0 comments on commit 51e448d

Please sign in to comment.