2015-12-08 47 views
0

每當我嘗試訪問模型時,都創建了名爲KlassAttribute。我收到以下錯誤,我不完全明白爲什麼,或者如何糾正這個問題。TypeError沒有將(我的類名):: ActiveRecord_Relation隱式轉換爲整數

KlassAttribute :: ActiveRecord_Relation的的隱式轉換成整數

當我加載在KlassAttribute軌道控制檯和類型它發生例如。我有3個模型和3個表格。 klass有許多klass_attributes,而klass_attribute有許多klass_values。

由於異常,KlassAttribute常量僅被部分加載。我認爲我的模型和模式是正確的,但是什麼導致我的問題?如果我在模型中省略「驗證」,則可以很好地訪問該模型。

Schema.rb

create_table "klasses", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

create_table "klass_attributes", force: :cascade do |t| 
    t.integer "klass_id" 
    t.string "name" 
    t.string "destination" 
    t.integer "priority" 
    t.integer "value_type", default: 0 
    t.datetime "created_at",    null: false 
    t.datetime "updated_at",    null: false 
end 

add_index "klass_attributes", ["klass_id"], name: "index_klass_attributes_on_klass_id" 

create_table "klass_values", force: :cascade do |t| 
    t.string "key" 
    t.string "value" 
    t.integer "klass_attribute_id" 
    t.datetime "created_at",   null: false 
    t.datetime "updated_at",   null: false 
end 

add_index "klass_values", ["klass_attribute_id"], name: "index_klass_values_on_klass_attribute_id" 

klass.rb

class Klass < ActiveRecord::Base 
    has_many :klass_attributes 
    accepts_nested_attributes_for :klass_attributes, allow_destroy: true 

    validates :name, presence: true, uniqueness: true, length: { maximum: 255 } 
end 

klass_attribute.rb

class KlassAttribute < ActiveRecord::Base 
    belongs_to :klass 
    has_many :klass_values 
    accepts_nested_attributes_for :klass_values, allow_destroy: true 

    validates :name, presence: true, length: { maximum: 255 } 
end 

klass_value.rb

class KlassValue < ActiveRecord::Base 
    belongs_to :klass_attribute 

    validates :key, length: { maximum: 255 } 
    validates :value, presence: true, length: { maximum: 255 } 
end 

回答

0

我想出了爲什麼我有這個錯誤。 我已經在KlassAttribute模型中聲明瞭一個枚舉,我沒有在上面的源代碼中包含它。

enum value_type: { array: 0, hash: 1} 

在此枚舉哈希鍵是錯誤的原因,所以我改名哈希更合適,瞧東西!

相關問題