【问题标题】:Rails 2.3.16 associations giving ArgumentError when using associationsRails 2.3.16 关联在使用关联时给出 ArgumentError
【发布时间】:2013-02-07 15:14:30
【问题描述】:

我正在将旧项目从 Rails 2.3.2 升级到 2.3.16(由于最近公开的漏洞),但是当我尝试访问任何模型上的 has_many 关联时,我得到来自sanitize_sqlArgumentError

class User
  has_many :games, :dependent => destroy
end

class Game
  belongs_to :user
end

当我尝试调用 Game.last.user 时,它会返回正确的 User 对象,但调用 User.last.games 时会出现以下错误堆栈:

ArgumentError: wrong number of arguments (2 for 1)
  from /home/username/application/shared/bundle/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/associations/association_proxy.rb:174:in `sanitize_sql'
  from /home/username/application/shared/bundle/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/associations/association_proxy.rb:174:in `send'
  from /home/username/application/shared/bundle/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/associations/association_proxy.rb:174:in `sanitize_sql'
  from /home/username/application/shared/bundle/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/associations/association_collection.rb:41:in `find'
  from /home/username/application/shared/bundle/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/associations/association_collection.rb:423:in `find_target'
  from /home/username/application/shared/bundle/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/associations/association_collection.rb:365:in `load_target'
  from /home/username/application/shared/bundle/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/associations/association_proxy.rb:140:in `inspect'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/irb.rb:310:in `output_value'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/irb.rb:159:in `eval_input'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/irb.rb:271:in `signal_status'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/irb.rb:155:in `eval_input'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/irb.rb:154:in `eval_input'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/irb.rb:71:in `start'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/irb.rb:70:in `catch'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/irb.rb:70:in `start'
  from /home/username/.rvm/rubies/ruby-1.8.7-p371/bin/irb:17

我在 Ruby 1.8.7 上运行 Rails 2.3.16;如果我遗漏了任何其他可能有助于诊断问题的详细信息,请告诉我。

编辑

我似乎暂时通过编辑activerecord-2.3.16/lib/active_record/associations/association_proxy.rb 文件中的错误行来解决这个问题,如下所示:

# Forwards the call to the reflection class.
def sanitize_sql(sql, table_name = @reflection.klass.quoted_table_name)
  @reflection.klass.send(:sanitize_sql, sql) #, table_name)
end

显然,我更喜欢更长期的解决方案,因为我不能 100% 确定这不会产生额外的连锁问题。

【问题讨论】:

  • 你的 ree 版本的补丁级别是多少?使用 ree1.8.7-2012.02 是否仍然出现相同的错误?我对 ree-1.8.7-2011.12 有一些问题
  • 我使用的是 ruby​​-1.8.7-p371,但我会试试 ree 看看是否有帮助。
  • 不,更改为 ree-1.8.7-2012.02 似乎并没有解决问题。

标签: ruby-on-rails associations rails-activerecord


【解决方案1】:

我也有同样的问题,但找不到是什么原因造成的。

我也得出了编辑这一行的结论。我做了一个猴子补丁初始化器:

module ActiveRecord
  module Associations
    class AssociationProxy #:nodoc:
      def sanitize_sql(sql, table_name = @reflection.klass.quoted_table_name)
        @reflection.klass.send(:sanitize_sql, sql)#, table_name)
      end
    end
  end
end

我希望我能尽快找到真正的原因,因为这不是必需的。您的问题还有什么进展吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    相关资源
    最近更新 更多