作成した結合テーブルから削除しようとしていますが、理解できない奇妙なエラーが発生します。
ここに私のモデルがあります
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>