NameError:未初始化的常量<model>:<column_name> </column_name> </model>

时间:2014-03-30 20:53:57

标签: ruby-on-rails ruby postgresql nameerror

我在Rails设置中遇到了一个奇怪的问题,这让我感到困惑。 我试图在用户和图书之间创建以下关系

class Book < ActiveRecord::Base   
  has_many :follows, :dependent => :destroy
  belongs_to :user
end

class Follow < ActiveRecord::Base
  attr_accessible :user_id,
                  :book_id,
                  :updated_at
  belongs_to :user
  belongs_to :book_id
  validates :user_id, presence: true
  validates :book_id, presence: true
end

class User < ActiveRecord::Base   
  has_many :books, :dependent => :destroy
  has_many :follows, :dependent => :destroy
end

直观地说,用户可以拥有多本书籍,并且可以关注其他书籍(来自其他用户) 我希望我的Follow模型能够跟踪这种关系

以下是我的数据库设置方式(PostgreSQL)

# \d follows
                                     Table "public.follows"
   Column   |            Type             |                      Modifiers                       
------------+-----------------------------+------------------------------------------------------
 id         | integer                     | not null default nextval('follows_id_seq'::regclass)
 user_id    | integer                     | 
 book_id    | integer                     | 
 created_at | timestamp without time zone | 
 updated_at | timestamp without time zone | 
Indexes:
    "follows_pkey" PRIMARY KEY, btree (id)
    "index_follows_on_book_id_and_user_id" UNIQUE, btree (book_id, user_id)
    "index_follows_on_book_id" btree (book_id)
    "index_follows_on_user_id" btree (user_id)

最后,当我打开Rails控制台并尝试创建Follow实例并分配book_id值时,它会在标题中吐出消息:

  

rails c   加载开发环境(Rails 4.0.0.rc1)   2.0.0p247:001&gt; f = Follow.new    =&GT; #   2.0.0p247:004&gt; f.book_id = 48   NameError:未初始化的常量Follow :: BookId       来自/Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0.rc1/lib/active_record/inheritance.rb:125:in compute_type' from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0.rc1/lib/active_record/reflection.rb:178:in klass&#39;       来自/Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0.rc1/lib/active_record/associations/association.rb:207:in raise_on_type_mismatch!' from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0.rc1/lib/active_record/associations/belongs_to_association.rb:11:in替换&#39 ;       来自/Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0.rc1/lib/active_record/associations/singular_association.rb:17:in writer' from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0.rc1/lib/active_record/associations/builder/association.rb:78:in book_id =& #39;       来自(irb):4       来自/Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0.rc1/lib/rails/commands/console.rb:90:in start' from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0.rc1/lib/rails/commands/console.rb:9:in start&#39 ;       来自/Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0.rc1/lib/rails/commands.rb:66:in <top (required)>' from bin/rails:4:in要求&#39;       来自bin / rails:4:在`&#39;

如果有人能说出正在发生的事情并给出一些提示,那就太棒了。 提前谢谢。

1 个答案:

答案 0 :(得分:3)

更改:

belongs_to :book_id

belongs_to :book

另外,您真的希望将book_id和user_id标记为可访问吗?这可能允许使用精心设计的帖子请求更新这些字段。