Rails Active Records SQLite3 :: SQLException:没有这样的列:subs.subscriber_id:SELECT“subs”。* FROM“subs”WHERE“subs”。“subscriber_id”=?

时间:2015-12-22 07:54:08

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 activerecord sqlite

我与Has_manySubscriber

使用Sub models关系
class Sub < ActiveRecord::Base

  belongs_to :Subscriber
end

class Subscriber < ActiveRecord::Base
  has_many :subs

end

将记录插入Sub表格时播放

我收到以下错误如下

错误记录

Rails Console.
 a=Subscriber.find(1)
  Subscriber Load (0.0ms)  SELECT  "subscribers".* FROM "subscribers" WHERE "subscribers"."id" = ? LIMIT 1  [["id", 1]]
=> #<Subscriber id: 1, name: "arun", created_at: "2015-12-22 07:38:34", updated_at: "2015-12-22 07:38:34">
irb(main):002:0> a.subs
  Sub Load (0.0ms)  SELECT "subs".* FROM "subs" WHERE "subs"."subscriber_id" = ?  [[nil, 1]]
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: subs.subscriber_id: SELECT "subs".* FROM "subs" WHERE "subs"."subscriber_id" = ?

2 个答案:

答案 0 :(得分:0)

使用

class Sub < ActiveRecord::Base
    belongs_to :subscriber
end

class Subscriber < ActiveRecord::Base 
    has_many :subs
end

使确定您已运行迁移。 请勿在关联中使用 CAPITAL

你也可以分享你的架构吗?

PS:请缩进你的代码。

答案 1 :(得分:0)

使用此belongs_to :subscriber

如果不起作用

将subscriber_id列添加到潜艇

键入以下rails命令

rails g migration AddSubscriber_idToSub subscriber_id:integer

然后

rake db:migrate