Ruby on rails 获取具有多个关联的字符串时出错,TypeError:无法将字符串转换为整数

Ruby on rails 获取具有多个关联的字符串时出错,TypeError:无法将字符串转换为整数,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,associations,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,Associations,注:完整代码可在 我有一个标签模型和一个文章模型,它们通过标签模型处于HABTM关系中。当我尝试在包含关联的同时对文章进行查找查询时,它失败了 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 "

注:完整代码可在

我有一个标签模型和一个文章模型,它们通过标签模型处于HABTM关系中。当我尝试在包含关联的同时对文章进行查找查询时,它失败了

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">] 
三种模式,

第1.rb条

Tag.rb

Tagging.rb


发现了这个问题,我在文章中有一个专栏名为hash,SQLite不喜欢这样

见:

class Article < ActiveRecord::Base
  has_many :taggings, :dependent => :destroy
  has_many :tags, :through => :taggings
class Tag < ActiveRecord::Base
  has_many :taggings, :dependent => :destroy
  has_many :articles, :through => :taggings
class Tagging < ActiveRecord::Base
  belongs_to :article
  belongs_to :tag