1

注意:全碼可以在http://github.com/basicxman/andrewhorsman.net錯誤獲取一個的has_many關聯時,類型錯誤:不能轉換成字符串整數

發現我有一個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 

Tag.rb

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

Tagging.rb

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

回答

相關問題