Vote count:
0
I've encountered a strange problem in my Rails setup, that is puzzling me to no end. 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)
# \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 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
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 `'
If someone can tell what's going on and give a few hints, that would be awesome. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire