我正在尝试从我创建的连接表中删除,但我遇到了一个我无法弄清楚的奇怪错误。
这是我的模型
class ArticlesUser < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :user
belongs_to :article
end
class Article < ActiveRecord::Base
attr_accessible :title
belongs_to :user
has_many :articles_users
has_many :likes, :through => :articles_users, :class_name => 'User', :source => :user
end
class User < ActiveRecord::Base
has_many :articles, :order => 'id DESC'
has_and_belongs_to_many :badges
has_many :articles_users
has_many :likes, :through => :articles_users, :class_name => 'Article', :source => :article
end
在 Rails 控制台中测试可以看到错误:
> a = Article.find(13)
> a.articles_users #works fine, returns an array of users who "like" the article
> a.articles_users.where(user_id: 3) #works as well
> a.articles_users.where(user_id: 3).first.destroy #this is where the error is thrown!
这里有错误:
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'articles_users.' in 'where clause': DELETE FROM `articles_users` WHERE `articles_users`.`` = NULL
为什么它似乎完全忽略了 where 哈希?请帮助我解决这个问题,我今天花了几个小时试图弄清楚。
谢谢!
编辑:
该articles_users
表有 2 列:article_id
和user_id
编辑:
这是从控制台复制和粘贴的:
1.9.3p194 :004 > a.articles_users.where(user_id: 3).first
ArticlesUser Load (0.7ms) SELECT `articles_users`.* FROM `articles_users` WHERE `articles_users`.`article_id` = 13 AND `articles_users`.`user_id` = 3 LIMIT 1
=> #<ArticlesUser user_id: 3, article_id: 13>