gpt4 book ai didi

ruby-on-rails - ruby和rails版本升级后has_many关系隐式转换报错

转载 作者:数据小太阳 更新时间:2023-10-29 08:36:19 33 4
gpt4 key购买 nike

我刚刚升级了我的 ruby​​ on rails 项目

ruby 1.9.3-p551 到 ruby 2.3.0

rails 3.2.13 到 rails 3.2.22(3-2-稳定)

升级前没有错误。

在我的用户模型中,为了管理用户的联系人,我有一些复杂的关系,在访问这些关系时,我遇到了一个no implicit conversion of nil into String错误。

关系定义为-

has_many :contacts, :foreign_key => 'owner_id'
has_many :pending_invited, conditions: {:'contacts.confirmed' => false, :'contacts.denied' => false }, through: :contacts, class_name: 'User', source: :user
has_many :invited, conditions: { :'contacts.confirmed' => true }, through: :contacts, class_name: 'User', source: :user

has_many :inverse_contacts, :foreign_key => 'user_id', :class_name => 'Contact'
has_many :pending_invited_by, conditions: {:'contacts.confirmed' => false, :'contacts.denied' => false }, through: :inverse_contacts, class_name: 'User', source: :owner
has_many :invited_by, conditions: {:'contacts.confirmed' => true }, through: :inverse_contacts, class_name: 'User', source: :owner

has_many :denied, conditions: {:'contacts.denied' => true }, through: :contacts, class_name: 'User', source: :user
has_many :denied_by, conditions: {:'contacts.denied' => true }, through: :inverse_contacts, class_name: 'User', source: :owner

使用 rails c 我可以通过运行获得完整的堆栈跟踪 -

u = User.first
u.pending_invited_by

这会产生以下错误-

TypeError: no implicit conversion of nil into String
from .bundle/gems/arel-3.0.3/lib/arel.rb:40:in `initialize'
from .bundle/gems/arel-3.0.3/lib/arel.rb:40:in `new'
from .bundle/gems/arel-3.0.3/lib/arel.rb:40:in `sql'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/join_helper.rb:47:in `block in sanitize'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/join_helper.rb:45:in `map'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/join_helper.rb:45:in `sanitize'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/association_scope.rb:94:in `block in add_constraints'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/association_scope.rb:39:in `each'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/association_scope.rb:39:in `each_with_index'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/association_scope.rb:39:in `add_constraints'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/association_scope.rb:31:in `scope'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/association.rb:99:in `association_scope'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/association.rb:88:in `scoped'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/has_many_through_association.rb:178:in `find_target'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/collection_association.rb:335:in `load_target'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
from .bundle/bundler/gems/rails-f85bbed4cdc1/activerecord/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
from .bundle/bundler/gems/rails-f85bbed4cdc1/railties/lib/rails/commands/console.rb:47:in `start'
from .bundle/bundler/gems/rails-f85bbed4cdc1/railties/lib/rails/commands/console.rb:8:in `start'
from .bundle/bundler/gems/rails-f85bbed4cdc1/railties/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

感谢任何帮助。

最佳答案

实际上,我今天遇到了这个。问题是现代 Ruby (2.3) 与古老的 ActiveRecord (3.2.22) 的结合。 Ruby 2.2 没有这个问题。

在 Ruby 2.3 中,Hash 响应 to_proc,这会触发 ActiveRecord 3.2 中的意外错误。

检查 https://github.com/rails/rails/issues/25010以获取更多详细信息/讨论。

我的临时解决方法是取消定义 Hash.to_proc,因为我 100% 确定我的代码或 gem 都没有使用它:

哈希类
undef_method:to_proc
结尾

关于ruby-on-rails - ruby和rails版本升级后has_many关系隐式转换报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36677092/

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