[英]NameError: uninitialized constant <Model>:<column_name>
I've encountered a strange problem in my Rails setup, that is puzzling me to no end. 我在Rails设置中遇到了一个奇怪的问题,这让我感到困惑。 I'm trying to create a following relation between Users and Books 我正在尝试在用户和书籍之间创建以下关系
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
Intuitively, a User can have many Books, and can follow other Books (from other Users) I wanted my Follow model to track that relationship 直观地说,用户可以拥有许多书籍,并且可以关注其他书籍(来自其他用户)我希望我的关注模型跟踪这种关系
Here is how my database is setup (PostgreSQL) 以下是我的数据库设置方式(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)
Finally, when I turn on Rails Console and try to create a Follow instance, and assign a book_id value, it spits out the message in the title: 最后,当我打开Rails控制台并尝试创建一个Follow实例,并分配一个book_id值时,它会在标题中吐出消息:
rails c Loading development environment (Rails 4.0.0.rc1) 2.0.0p247 :001 > f = Follow.new => # 2.0.0p247 :004 > f.book_id = 48 NameError: uninitialized constant Follow::BookId from /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' from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0.rc1/lib/active_record/associations/association.rb:207:inraise_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
rails c加载开发环境(Rails 4.0.0.rc1)2.0.0p247:001> f = Follow.new =>#2.0.0p247:004> f.book_id = 48 NameError:uninitialized constant Follow :: BookId from / Users / minghuazhao / .rvm / gems / ruby-2.0.0-p247 / gems / activerecord-4.0.0.rc1 / lib / active_record / inheritance.rb:125:incompute_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'from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems /activerecord-4.0.0.rc1/lib/active_record/associations/association.rb:207:inraise_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
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
replace' from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0.rc1/lib/active_record/associations/singular_association.rb:17:inwriter' 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=' from (irb):4 from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0.rc1/lib/rails/commands/console.rb:90:instart' from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0.rc1/lib/rails/commands/console.rb:9:in
start' from /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
require' from bin/rails:4: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
替换'from / Users / minghuazhao / .rvm / gems / ruby-2.0.0-p247 / gems / activerecord-4.0.0.rc1 / lib / active_record / associations / singular_association.rb:17:inwriter' 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 ='from(irb):4 from /Users/minghuazhao/.rvm/ gems / ruby-2.0.0-p247 / gems / railties-4.0.0.rc1 / lib / rails / commands / console.rb:90:instart' from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0.rc1/lib/rails/commands/console.rb:9:in
start'from /Users/minghuazhao/.rvm/gems/ruby-2.0.0-p247/gems/ railties-4.0.0.rc1 / lib / rails / commands.rb:66:在<top (required)>' from bin/rails:4:in
require'from<top (required)>' from bin/rails:4:in
in`
If someone can tell what's going on and give a few hints, that would be awesome. 如果有人能分辨出发生了什么并给出一些提示,那就太棒了。 Thanks in advance. 提前致谢。
Change: 更改:
belongs_to :book_id
to 至
belongs_to :book
Also, do you really want book_id and user_id to be marked as accessible? 另外,您真的希望将book_id和user_id标记为可访问吗? This may allow those fields to be updated with crafted post requests. 这可能允许使用精心设计的帖子请求更新这些字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.