sway
sway

Reputation: 373

ActiveRecord Unknown Attribute error (Sinatra)

Where am I going wrong here? I get this error:

unknown attribute: bar_id

I have these two classes:

class Foo < ActiveRecord::Base
  belongs_to :bar
end

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

I'm getting this error when I try to create new Foos using:

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

Upvotes: 1

Views: 396

Answers (1)

Leo Correa
Leo Correa

Reputation: 19809

According to your example, you're not trying to create Bars but you're trying to create a Foo record that's associated to a Bar object. If you have a belongs_to association to Bar from Foo, then Foo needs to have a column called bar_id to reference which Bar a certain Foo record belongs_to.

Upvotes: 1

Related Questions