2010-10-22 49 views
1

我一直試圖在Rails 3中設置單表繼承模型,其中父類也包含has_many關係。不幸的是我無法讓它工作。這裏有三個班爲例:Rails 3單表繼承w/has_many問題

class Article < ActiveRecord::Base 
    has_many :paragraphs, :dependent => :destroy, :autosave => true 
end 

class Paragraph < ActiveRecord::Base 
    belongs_to :article 
end 

class SportsArticle < Article 
end 

而這裏將用於設置此遷移:

class AddTables < ActiveRecord::Migration 
    def self.up 
     create_table :articles do |t| 
      t.string  :type,  :null => false # for STI 
      t.string  :title,  :null => false 
      t.timestamps 
     end 

     create_table :paragraphs do |t| 
      t.references :article, :null => false 
      t.timestamps 
     end 
    end 

    def self.down 
     drop_table :articles 
     drop_table :paragraphs 
    end 
end 

當我將它設置了這種方式,我嘗試創建一個新的SportsArticle ,通過做說以下內容:

SportsArticle.create(:title => "Go Giants") 

我總是得到以下錯誤:

「類型錯誤:無法轉換成字符串整數」

我不知道如何解決這個問題,並試圖在網上找到一個解決辦法都無濟於事。有STI模型經驗的人會看到有什麼不對嗎?這裏的鏈接到文件的創建方法,如果將診斷問題有所幫助: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-create

回答

0

的錯誤是由於命名衝突造成的。我爲我的一個模型使用了一個名爲「屬性」的名稱,這導致了問題。最終診斷問題的提示來自Rails Association Documentation

0

嘗試重命名:種類別的東西,如:article_type

如:

t.string  :article_type,  :null => false # for STI 
+0

這沒有奏效。在rails文檔中,它說你應該命名列「type」來實現STI http://api.rubyonrails.org/classes/ActiveRecord/Base.html – Abe 2010-10-23 19:57:52

+0

感謝porkeypop的響應,它最終最終成爲了一種不同類型的命名衝突。 – Abe 2010-10-24 21:08:18