【问题标题】:Error when fetching a has_many association, TypeError: can't convert String into Integer获取 has_many 关联时出错,TypeError: can't convert String into Integer
【发布时间】:2011-07-02 02:33:56
【问题描述】:

注意:完整代码可以在http://github.com/basicxman/andrewhorsman.net找到

我有一个 Tag 模型和一个 Article 模型,它们通过 Tagging 模型处于 HABTM 关系中。当我尝试在包含关联的同时对 Article 执行 find 查询时,它会失败。

ruby-1.9.2-p136 :003 > Article.find(:all, :include => :tags)
  Article Load (0.3ms)  SELECT "articles".* FROM "articles" 
  Tagging Load (0.2ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."article_id" IN (1)
TypeError: can't convert String into Integer
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader/association.rb:83:in `[]'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader/association.rb:83:in `associated_records_by_owner'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader/collection_association.rb:13:in `preload'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader/association.rb:19:in `run'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:128:in `block (2 levels) in preload_one'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:127:in `each'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:127:in `block in preload_one'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:126:in `each'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:126:in `preload_one'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:105:in `preload'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:94:in `block in run'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:94:in `each'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:94:in `run'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader/through_association.rb:31:in `through_records_by_owner'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader/through_association.rb:15:in `associated_records_by_owner'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader/has_many_through.rb:8:in `associated_records_by_owner'
... 8 levels...
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:94:in `block in run'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:94:in `each'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/associations/preloader.rb:94:in `run'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:119:in `block in to_a'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:118:in `each'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:118:in `to_a'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/relation/finder_methods.rb:155:in `all'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/relation/finder_methods.rb:105:in `find'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/relation/finder_methods.rb:101:in `find'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.1.0.rc4/lib/active_record/base.rb:444:in `find'
  from (irb):3
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
  from /Users/basicxman/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'ruby-1.9.2-p136 :004 > 

这是当前记录的样子:

ruby-1.9.2-p136 :005 > Article.all
  Article Load (0.3ms)  SELECT "articles".* FROM "articles" 
 => [#<Article id: 1, title: "testing", author: "test", content: "ohi", published_at: nil, stage: 0, last_commit_date: nil, created_at: "2011-07-01 07:06:08", updated_at: "2011-07-01 07:06:08", content_html: nil, hash: "84faf770-85de-012e-112f-109add4b71f4">] 
ruby-1.9.2-p136 :006 > Tagging.all
  Tagging Load (0.3ms)  SELECT "taggings".* FROM "taggings" 
 => [#<Tagging id: 1, article_id: 1, tag_id: 1, created_at: "2011-07-01 07:06:19", updated_at: "2011-07-01 07:06:19">] 
ruby-1.9.2-p136 :007 > Tag.all
  Tag Load (0.3ms)  SELECT "tags".* FROM "tags" 
 => [#<Tag id: 1, keyword: "testing", created_at: "2011-07-01 07:06:19", updated_at: "2011-07-01 07:06:19">] 

还有三个模型,

Article.rb

class Article < ActiveRecord::Base
  has_many :taggings, :dependent => :destroy
  has_many :tags, :through => :taggings

标签.rb

class Tag < ActiveRecord::Base
  has_many :taggings, :dependent => :destroy
  has_many :articles, :through => :taggings

标记.rb

class Tagging < ActiveRecord::Base
  belongs_to :article
  belongs_to :tag

【问题讨论】:

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


    【解决方案1】:

    发现问题,我在Article 中有一个名为hash 的列,SQLite 不喜欢这个。

    见:SQLite3 Error - cannot rollback - no transaction is active

    【讨论】:

      猜你喜欢
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 2014-06-12
      相关资源
      最近更新 更多