Problem:
The db:mirage was failing because it couldn’t find belongs_to_host
# foreman-rake db:migrate
API controllers newer than Apipie cache! Run apipie:cache rake task to regenerate cache.
rake aborted!
NameError: undefined local variable or method `belongs_to_host' for #<Class:0x00005559c6e37cc0>
Did you mean? belongs_to

# foreman-rake apipie:cache
rake aborted!
NameError: undefined local variable or method `belongs_to_host' for #<Class:0x0000561285345dd0>

I did a search:
root@host:/usr/share/gems
# find . -type f -exec grep -l belongs_to_host {} \;
./gems/foreman_chef-0.10.0/app/models/foreman_chef/cached_run_list.rb
root@host:/usr/share/gems/gems/foreman_chef-0.10.0/app/models/foreman_chef
# rpm -qf cached_run_list.rb
rubygem-foreman_chef-0.10.0-2.fm2_5.el8.noarch
I found that nothing depended on the rubygem-foreman_chef-0.10.0-2.fm2_5.el8.noarch so I removed it and then the db:migrate would work.
Later when I ran the foreman-installer it was reinstalled then it failed again:
2022-06-27 14:41:41 [ERROR ] [configure] '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]
I then determined that I need to tell it to not enable that plugin:
foreman-installer --no-enable-foreman-plugin-chef
And it worked.
Expected outcome:
I didn’t expect to see any problems with db:migrate.
Foreman and Proxy versions:
3.1.3 going to
Upgraded:
foreman-3.2.1-1.el8.noarch
foreman-cli-3.2.1-1.el8.noarch
foreman-debug-3.2.1-1.el8.noarch
foreman-dynflow-sidekiq-3.2.1-1.el8.noarch
foreman-installer-1:3.2.1-2.el8.noarch
foreman-postgresql-3.2.1-1.el8.noarch
foreman-proxy-3.2.1-1.el8.noarch
foreman-selinux-3.2.1-1.el8.noarch
foreman-service-3.2.1-1.el8.noarch
rubygem-dynflow-1.6.4-1.fm3_2.el8.noarch
rubygem-foreman-tasks-6.0.1-1.fm3_2.el8.noarch
rubygem-foreman_bootdisk-19.0.3-1.fm3_2.el8.noarch
rubygem-foreman_discovery-20.0.1-1.fm3_2.el8.noarch
rubygem-foreman_puppet-3.0.7-1.fm3_2.el8.noarch
rubygem-hammer_cli-3.2.0-1.20220214173651git27087bf.el8.noarch
rubygem-hammer_cli_foreman-3.2.0-1.20220214175116git323f240.el8.noarch
rubygem-hammer_cli_foreman_puppet-0.0.5-1.fm3_2.el8.noarch
rubygem-puma-5.5.2-1.el8.x86_64
Foreman and Proxy plugin versions:
Distribution and version:
Other relevant data:
1 Like
Hi,
I have the same problem with this upgrade,
$ /usr/sbin/foreman-rake db:migrate
API controllers newer than Apipie cache! Run apipie:cache rake task to regenerate cache.
rake aborted!
NameError: undefined local variable or method belongs_to_host' for #<Class:0x000000000d730598> Did you mean? belongs_to /opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/dynamic_matchers.rb:22:in
method_missing’
/opt/theforeman/tfm/root/usr/share/gems/gems/foreman_chef-0.10.0/app/models/foreman_chef/cached_run_list.rb:5:in <class:CachedRunList>' /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_chef-0.10.0/app/models/foreman_chef/cached_run_list.rb:2:in
module:ForemanChef’
/opt/theforeman/tfm/root/usr/share/gems/gems/foreman_chef-0.10.0/app/models/foreman_chef/cached_run_list.rb:1:in <top (required)>' /opt/theforeman/tfm/root/usr/share/gems/gems/polyglot-0.3.5/lib/polyglot.rb:65:in
require’
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:324:in block in require' /opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:291:in
load_dependency’
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:324:in require' /opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:411:in
block in require_or_load’
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:40:in block in load_interlock' /opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies/interlock.rb:14:in
block in loading’
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/concurrency/share_lock.rb:151:in exclusive' /opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies/interlock.rb:13:in
loading’
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:40:in load_interlock' /opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:389:in
require_or_load’
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:544:in load_missing_constant' /opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:214:in
const_missing’
/opt/theforeman/tfm/root/usr/share/gems/gems/foreman_chef-0.10.0/lib/foreman_chef/engine.rb:65:in block (2 levels) in <class:Engine>' /usr/share/foreman/app/registries/foreman/plugin.rb:100:in
instance_eval’
/usr/share/foreman/app/registries/foreman/plugin.rb:100:in register' /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_chef-0.10.0/lib/foreman_chef/engine.rb:49:in
block in class:Engine’
/opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/initializable.rb:32:in instance_exec' /opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/initializable.rb:32:in
run’
/usr/share/foreman/config/initializers/0_print_time_spent.rb:45:in block in run' /usr/share/foreman/config/initializers/0_print_time_spent.rb:17:in
benchmark’
/usr/share/foreman/config/initializers/0_print_time_spent.rb:45:in run' /opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/initializable.rb:61:in
block in run_initializers’
/opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/initializable.rb:60:in run_initializers' /opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/application.rb:363:in
initialize!’
/opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/railtie.rb:190:in public_send' /opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/railtie.rb:190:in
method_missing’
/usr/share/foreman/config/environment.rb:5:in <top (required)>' /opt/theforeman/tfm/root/usr/share/gems/gems/polyglot-0.3.5/lib/polyglot.rb:65:in
require’
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:324:in block in require' /opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:291:in
load_dependency’
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.7/lib/active_support/dependencies.rb:324:in require' /opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/application.rb:339:in
require_environment!’
/opt/theforeman/tfm/root/usr/share/gems/gems/railties-6.0.3.7/lib/rails/application.rb:523:in block in run_tasks_blocks' /opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/exe/rake:27:in
<top (required)>’
Tasks: TOP => db:migrate => db:load_config => environment
(See full trace by running task with --trace)
and
foreman-installer --no-enable-foreman-plugin-chef
it’s not enough to fix the error, in my case 
- root@ /opt/theforeman/tfm/root/usr/share/gems $ find . -type f -exec grep -l belongs_to_host {} ;
./gems/foreman_chef-0.10.0/app/models/foreman_chef/cached_run_list.rb
./gems/foreman_ansible-7.1.0/app/models/host_ansible_role.rb
./gems/foreman_remote_execution-6.0.0/app/models/targeting_host.rb
It would be nice to know if we should delete or change belongs_to_host in those files.
example:
$ cat ./gems/foreman_remote_execution-6.0.0/app/models/targeting_host.rb
class TargetingHost < ApplicationRecord
belongs_to :targeting
belongs_to_host
validates :targeting, :presence => true
validates :host, :presence => true
end
This file /usr/share/foreman/app/models/host/hostmix.rb(foreman-3.1.2-2.el7.noarch) where belongs_to_host is defined, doesn’t exist anymore with foreman-3.2.1-1.el7.noarch.
This variable is defined now in /usr/share/foreman/app/models/concerns/host_mix.rb, but apparently this file is not recognized when this error appears:
NameError: undefined local variable or method belongs_to_host'
…
I recently upgrade from 2.5->3.0->3.1->3.2 on a server that I had previously had the chef plugin enabled/installed. I am not actively using the chef capability in Foreman yet, so I disabled it via foreman-installer.
I recall seeing errors like you are getting and was able to resolve it by removing the following packages: tfm-rubygem-foreman_chef tfm-rubygem-chef-api tfm-rubygem-smart_proxy_chef
See if those packages are installed and consider removing them.
Good Luck
I understood now, the correct sequence:
…
** yum remove tfm-rubygem-foreman_chef -y
** foreman-rake db:migrate
** foreman-installer --no-enable-foreman-plugin-chef
…
In this way, the upgrade could be completed in my case.
Thank you very much jwilliam and frostygresh 