ruby-on-rails – 没有这样的列:以下多对多关联中的书签

我有一个帖子和一个用户模型.我正在尝试添加书签模式,以便用户可以为帖子添加书签.我正在使用连接模型:

schema.rb:

create_table "bookmarks",:force => true do |t|
  t.integer  "bookmarker_id"
  t.integer  "bookmarked_id"
  t.datetime "created_at",:null => false
  t.datetime "updated_at",:null => false
end

bookmark.rb:

class Bookmark < ActiveRecord::Base
  attr_accessible :bookmarked_id

  belongs_to :bookmarker,class_name: "User"
  belongs_to :bookmarked,class_name: "Post"

  validates :bookmarker_id,presence: true
  validates :bookmarked_id,presence: true
end

post.rb:

has_many :bookmarks,:dependent => :destroy
has_many :bookmarkers,:through => :bookmarks

user.rb:

has_many :bookmarks,:dependent => :destroy
has_many :bookmarked_posts,:through => :bookmarks,source: :bookmarked

我试着测试终端中的关联,但我明白了:

1.9.3-p0 :007 > user.bookmarks
  Bookmark Load (0.4ms)  SELECT "bookmarks".* FROM "bookmarks" WHERE "bookmarks"."user_id" = 1
ActiveRecord::StatementInvalid: sqlite3::sqlException: no such column: bookmarks.user_id: SELECT "bookmarks".* FROM "bookmarks"  WHERE "bookmarks"."user_id" = 1

可能是什么问题呢?

解决方法

你没有指定foreign_key,异常清楚地说

no such column: bookmarks.user_id

加上

has_many :bookmarks,:foreign_key => 'bookmarker_id'

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...