我正在尝试通过关联使用update_all,我收到mysql错误,有人知道为什么会这样吗?
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))
答案 0 :(得分:2)
http://dev.rubyonrails.org/ticket/5353
使用has_many:through和使用update all看起来n-n关联存在问题。似乎没有做任何事情。
1-n关联确实起作用。
错误?
答案 1 :(得分:2)
dev.rubyonrails将它的门票移到了github的问题跟踪器上。以下是已移动的链接:https://github.com/rails/rails/issues/522
@nolman在票证上发布了这个帮助
@dicoden和@square上的我在这方面配对,我们能够按照以下方式组合:class Comment class << self def trash_all sql = "UPDATE #{quoted_table_name} " add_joins!(sql, {}) sql << "SET #{sanitize_sql_for_assignment({:trashed => true})} " add_conditions!(sql, {}) connection.execute(sql) end end end
现在您可以致电todolist.comments(:conditions =&gt; {:trashed =&gt; false})。trash_all 这导致以下SQL:
UPDATE `comments` INNER JOIN `todos` ON `todos`.id = `comments`.todo_id SET `trashed` = 1 WHERE (`comments`.`trashed` = 0 AND `todos`.`todolist_id` = 968316918)
希望这有帮助!