本文介绍了通过关联进行update_all的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过关联使用update_all,并且出现mysql错误,有人知道为什么吗?

I am trying to use update_all through an association, and i am getting mysql errors, anyone know why please?

class Basket < ActiveRecord::Base
  has_many :basket_items
  has_many :articles, :through => :basket_items

  def activate_articles
    articles.update_all :active => true
  end
end

class BasketItem < ActiveRecord::Base
  belongs_to :basket
  belongs_to :item
  belongs_to :article
end


Mysql::Error: Unknown column 'basket_items.basket_id' in 'where clause': UPDATE `articles` SET `active` = 1 WHERE ((`basket_items`.basket_id = 114)) 


推荐答案

使用has_many:through并使用update all看起来,nn关联存在问题。似乎什么都没做。

Looks like there was a problem with n-n associations using has_many :through and using update all. Nothing seems to have been done.

1-n的确起作用了。

1-n associations do appear to work.

臭虫?

这篇关于通过关联进行update_all的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 16:50