【问题标题】:ActiveRecord Unknown Attribute error (Sinatra)ActiveRecord 未知属性错误 (Sinatra)
【发布时间】:2013-04-08 08:17:11
【问题描述】:

我哪里错了?我收到此错误:

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")

【问题讨论】:

    标签: ruby activerecord sinatra


    【解决方案1】:

    根据您的示例,您不是在尝试创建 Bars,而是在尝试创建与 Bar 对象关联的 Foo 记录。如果你有一个从FooBarbelongs_to 关联,那么Foo 需要有一个名为bar_id 的列来引用哪个Bar 某个Foo 记录belongs_to

    【讨论】:

    • 是的,我的意思是新的 Foos。我已经适当地更新了这个问题。我认为将belongs_tohas_many 放在正确的位置会在我的foos 表中为我处理bar_id。我添加了bar_id,它成功了!谢谢狮子座。
    猜你喜欢
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多