Rails could not find the inverse association in has many relation

8.3k views Asked by At

I have two models (one to many relationship between them) in rails 5, One is ScholarshipGroup and Scholarship model with Admin namespace.

ScholarshipGroup Model:

class Admin::ScholarshipGroup < ApplicationRecord    
  has_many :admin_scholarships, :class_name =>   'Admin::Scholarship',inverse_of: :admin_scholarship_group
end

And Scholarship Model:

class Admin::Scholarship < ApplicationRecord
  belongs_to :group, :class_name=> 'Admin::ScholarshipGroup',   inverse_of: 'admin_scholarships'      
end

I want to need data for all scholarships under a ScholarshipGroup. But when I run this query from rails console:

$ Admin::ScholarshipGroup.first.admin_scholarships.first

But it gives me this error:

Admin::ScholarshipGroup Load (0.3ms)  SELECT  "admin_scholarship_groups".* FROM "admin_scholarship_groups" ORDER BY "admin_scholarship_groups"."id" ASC LIMIT ?  [["LIMIT", 1]]
ActiveRecord::InverseOfAssociationNotFoundError: Could not find the inverse association for admin_scholarships (:admin_scholarship_group in Admin::Scholarship)
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activerecord-5.0.0.1/lib/active_record/reflection.rb:202:in `check_validity_of_inverse!'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activerecord-5.0.0.1/lib/active_record/reflection.rb:402:in `check_validity!'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activerecord-5.0.0.1/lib/active_record/associations/association.rb:25:in `initialize'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activerecord-5.0.0.1/lib/active_record/associations.rb:235:in `new'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activerecord-5.0.0.1/lib/active_record/associations.rb:235:in `association'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activerecord-5.0.0.1/lib/active_record/associations/builder/association.rb:111:in `admin_scholarships'
        from (irb):34
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
        from /home/ubuntu/workspace/bin/rails:9:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
        from /usr/local/rvm/gems/ruby-2.3.1@college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
        from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from -e:1:in `<main>'

Why do return this type error ? Can anyone suggest me the solution of this error ?

1

There are 1 answers

8
Thanh On BEST ANSWER

You defined belongs_to :group in Admin::Scholarship model.

You can try:

has_many :admin_scholarships, :class_name => 'Admin::Scholarship',inverse_of: :group, foreign_key: 'group_id'