【问题标题】:Weird Deletion null error奇怪的删除空错误
【发布时间】:2012-07-26 04:06:52
【问题描述】:

我正在尝试从我创建的连接表中删除,但遇到了一个我无法弄清楚的奇怪错误。

这是我的模型

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_iduser_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> 

【问题讨论】:

  • a.articles_users.where(user_id: 3).first 的输出是什么
  • @priteshJ 刚刚用您的问题的答案更新了问题。

标签: ruby-on-rails-3


【解决方案1】:

我尝试并重现了您的问题。

从您的输出看来,您在 ArticlesUser 中没有 id 列 这是问题的原因。

=> #<ArticlesUser user_id: 3, article_id: 13> 

我尝试将 id 列添加到表中,它的工作原理很吸引人。 创建了一个新的迁移并添加它

 add_column :articles_users, :id, :primary_key

当你销毁一个记录时,rails 使用它的 id 作为参考从数据库中删除它。

检查示例。

 DELETE FROM `articles_users` WHERE `articles_users`.`id` = 1

【讨论】:

  • 我认为在连接表上有一个 ID 列是不好的做法?
  • 好像 Rails 在内部以某种方式使用它
【解决方案2】:

如果您有兴趣,我找到了一个没有主键的解决方案。

编辑:非常接近:Deleting just a join table record in Ruby on Rails,但我相信我的解决方案更加对称。

首先我关注了其中的一部分:http://ngauthier.com/2010/11/restful-many-to-many-relationships-in-rails.html 我只在我的 config/routes.rb 中更改了这一行:

resources :users

进入这个:

resources :users do
  resources :articles, :controller => 'ArticleUsers', :only => [:index, :create, :destroy]
end

然后生成您的 ArticleUsers 控制器并创建这样的方法:

def delete_article
  @user.articles.delete(@article)
  redirect_to user_articles_path
end

嗯,你还需要像这样的过滤器 @user :

def find_user
  @user = User.find(params[:user_id]
end

和@article 类似的一个(注意,如果您按照上面的链接进行操作,您可能必须使用 params([:id]) 而不是 params([:article_id]))

然后在你的 routes.rb 中添加:

match '/users/:user_id/articles/delete_article/:id', :controller => 'ArticleUsers', :action => 'delete_article'

请注意,如果您使用的不是 Rails 3,而是 Rails 4,则应将“match”替换为“map.connect”。

如果你用 slim 写,那么在你的索引中,如果你的@items 包含所有用户的文章:

 - @items.each do |articleuser|
   tr
     td= articleuser.article.title

     td
       = link_to "article/delete_article/#{articleuser.article_id}",\
         data: { confirm: 'Confirm ?' } do
         i.icon-trash.icon.icon-large

这适用于我必须在表格中显示链接的情况,但您可以轻松地将其调整为适合您的情况。

这种方法避免使用真正想要ID的destroy方法。

我花了一些时间和几个资源来解决这个问题,我希望它可以帮助其他人!

【讨论】:

    【解决方案3】:

    快速浏览一下..我也遇到了同样的问题,但我在我的代码中尝试了这个 -:

    @cart = Cart.where(:user_id => current_user.id).first
    @item = @cart.cart_items.where(:product_id => params[:product_id]).first
    @item.destroy
    

    或者在你的代码中你可以尝试使用这个

    @a=a.articles_users.where(user_id: 3).first
    @a.destroy
    

    试试看

    【讨论】:

    • @Deekor 在控制台中检查此行是否有效 a.articles_users.where(user_id: 3).first
    猜你喜欢
    • 1970-01-01
    • 2019-09-09
    • 2020-12-01
    • 2013-10-04
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    相关资源
    最近更新 更多