gpt4 book ai didi

ruby - Rails 找不到反向关联有很多关系

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:20 24 4
gpt4 key购买 nike

我在 Rails 5 中有两个模型(它们之间的一对多关系),一种是带有 Admin 命名空间的 ScholarshipGroup 和 Scholarship 模型。

奖学金团体模式:

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

和奖学金模式:

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

我需要 ScholarshipGroup 下所有奖学金的数据。但是当我从 Rails 控制台运行这个查询时:

$ Admin::ScholarshipGroup.first.admin_scholarships.first

但它给了我这个错误:

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>'

为什么会返回这种类型的错误?谁能建议我解决此错误的方法?

最佳答案

您在 Admin::Scholarship 模型中定义了 belongs_to :group

你可以试试:

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

关于ruby - Rails 找不到反向关联有很多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41278716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com