我刚刚升级了我的 Ruby on Rails 项目
ruby 1.9.3-p551 到 ruby 2.3.0
和
rails 3.2.13 到 rails 3.2.22(3-2-稳定)
升级之前没有错误。
在我的用户模型中,我有一些复杂的关系来管理用户的联系人,并且在访问这些关系时,我收到没有将 nil 隐式转换为字符串错误。
关系定义为 -
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% 确定我的代码或 gems 还没有使用它:
类哈希
undef_method :to_proc
结尾
关于ruby-on-rails - 升级ruby和rails版本后has_many关系隐式转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36677092/