不能重新从控制器的ActiveRecord协会 [英] Can't reload ActiveRecord association from controller
问题描述
花了一天的工作在这。
我
class Box
has_many :users, :through => :subscriptions
end
我也有自定义的 insert_new_users
和 associate_with它使用多个INSERT迅速做好自己的工作(new_users)
方法。无论如何,他们做工精细。我也有这条线在associate_with方法的末尾:
I also have custom insert_new_users
and associate_with(new_users)
methods which use multiple INSERT to do their job quickly. Anyway, they work fine. I also have this line at the end of "associate_with" method:
def associate_with
# mysql INSERT here
self.users(true) # Should force reload
end
这在测试环境(包括控制器和模型测试)运行时,将按预期,也未能如预期,如果我删除真正
参数,它强制重装。这也从脚本/控制台
在发展,如果我 update_attributes方法
的样板工程。但在开发或生产时失败,我从控制器试图 update_attributes方法
。它根本没有重装协会,我可以看到它在日志中,它说:CACHE(0.0ms)此查询。
It works as expected when running in test environment (both controller and model tests) and it fails as expected if I remove the true
argument, which forces the reload. It also works from script/console
in development if I update_attributes
the model. But fails in development or production when I'm trying to update_attributes
from controller. It simply does not reload associations and I can see it in logs, where it says "CACHE (0.0ms)" for this query.
怪异的事情 - 它的工作之前,我不能确定它停止工作的瞬间,由于一些原因。我希望也许有人知道,这怎么可能。
The weird thing - it worked before and I can't identify the moment it stopped working due to some reasons. I was hoping maybe someone knows how is this possible.
推荐答案
您所看到的ActiveRecord的SQL查询缓存的影响。
You're seeing the effects of the ActiveRecord SQL query cache.
无论换到用户pre-INSERT引用
协会在调用未缓存
self.class.uncached do
# ...
end
这将从缓存块内的任何查询的结果停止ActiveRecord的。如果你有很多地方code,这可能是讨厌的。
This will stop ActiveRecord from caching the results of any queries inside the block. If you have code in many places, this may be annoying.
您也可以致电 connection.clear_query_cache
清除缓存你与你的插入完成后。
You can also clear the cache after you are done with your inserts by calling connection.clear_query_cache
.
这篇关于不能重新从控制器的ActiveRecord协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!