2013-04-08 72 views
1

我在哪裏錯了?我得到這個錯誤:ActiveRecord未知屬性錯誤(Sinatra)

unknown attribute: bar_id 

我有以下兩類:

class Foo < ActiveRecord::Base 
    belongs_to :bar 
end 

class Bar < ActiveRecord::Base 
    has_many :bazs 
    has_many :foos 
end 

,當我嘗試創建新FOOS使用我得到這個錯誤:

@bar = Bar.find(1) 
@bar.foos.create(:attribute1 => "a", 
        :attribute2 => "b") 

回答

1

根據您的例如,您並未嘗試創建Bars,但您正嘗試創建與Bar對象關聯的Foo記錄。如果您有一個belongs_to關聯到,那麼Foo需要有一個名爲bar_id的列來引用哪個Bar某個Foo記錄belongs_to

+0

是的,我的意思是新的Foos。我已經適當更新了這個問題。我認爲在正確的位置擁有'belongs_to'和'has_many'會在我的'foos'表中爲我處理'bar_id'。我添加了'bar_id',它工作正常!感謝Leo。 – sway 2013-04-08 00:20:30