Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails有许多返回空数组的关联_Ruby On Rails_Ruby On Rails 4_Activerecord_Rails Activerecord - Fatal编程技术网

Ruby on rails Rails有许多返回空数组的关联

Ruby on rails Rails有许多返回空数组的关联,ruby-on-rails,ruby-on-rails-4,activerecord,rails-activerecord,Ruby On Rails,Ruby On Rails 4,Activerecord,Rails Activerecord,我们有一个运行在Rails 4.0.12上的遗留应用程序,我想知道这是否是一些奇怪的ActiveRecord错误,后来已经解决了 这是我的模型: class Gathering < ActiveRecord::Base has_many :gathering_contents, dependent: :destroy end 我看到的问题是,在重新加载之前,关联返回一个空数组。控制台输出示例: g = Gathering.last g.gathering_contents G

我们有一个运行在Rails 4.0.12上的遗留应用程序,我想知道这是否是一些奇怪的ActiveRecord错误,后来已经解决了

这是我的模型:

class Gathering < ActiveRecord::Base
  has_many :gathering_contents, dependent: :destroy
end
我看到的问题是,在重新加载之前,关联返回一个空数组。控制台输出示例:

g = Gathering.last    
g.gathering_contents
GatheringContent Load (0.6ms)  SELECT "gathering_contents".* FROM "gathering_contents" WHERE "gathering_contents"."gathering_id" = 'f6b6a7a7-7f51-4c56-af95-42567f7bc15b'  [["gathering_id", "f6b6a7a7-7f51-4c56-af95-42567f7bc15b"]]
=> #<ActiveRecord::Associations::CollectionProxy []>
运行原始SQL查询将返回两个结果。重新加载关联也会返回两个结果:

g.gathering_contents.reload
GatheringContent Load (4.7ms)  SELECT "gathering_contents".* FROM "gathering_contents" WHERE "gathering_contents"."gathering_id" = 'f6b6a7a7-7f51-4c56-af95-42567f7bc15b'  [["gathering_id", "f6b6a7a7-7f51-4c56-af95-42567f7bc15b"]]
=> #<ActiveRecord::Associations::CollectionProxy [#<GatheringContent id: "5e82a73c-036b-4ff1-9af3-2b19c2f1746b", gathering_id: "f6b6a7a7-7f51-4c56-af95-42567f7bc15b", course_id: "ad3eb286-3617-46bd-a10d-6d09cca23b64", path_id: nil, created_at: "2016-03-30 12:52:35", updated_at: "2016-03-30 12:52:35", public_course_id: nil, company_course_id: "ad3eb286-3617-46bd-a10d-6d09cca23b64", user_id: "33626432-0fa0-439f-9920-9f62ff26ffc6", admin_id: nil, conversation_id: nil>, #<GatheringContent id: "3ef08d57-d294-48a2-a46d-eab4b1a43132", gathering_id: "f6b6a7a7-7f51-4c56-af95-42567f7bc15b", course_id: nil, path_id: nil, created_at: "2016-04-01 15:19:27", updated_at: "2016-04-01 15:19:27", public_course_id: nil, company_course_id: nil, user_id: "458fa79f-4d1f-450b-bf4a-091b1183f86c", admin_id: nil, conversation_id: "8dbe18e7-a78d-45ca-9e3b-8fa403675f0b">]>

我没有一个好的理论来解释为什么在重新加载之前查询返回一个空结果。感谢您的任何意见

当您运行g.gathering\u contents.all或g.gathering\u contents.first时会发生什么?当您调用g.gathering\u contents时,Rails似乎正在返回关联缓存。重新加载它会清除缓存并再次命中数据库。调用g.collecting_contents.to_a返回[],首先返回nil@事实上,缓存的关联是我唯一的工作假设。但是,如果我刚刚通过rails c启动了一个新的控制台进程,那么缓存从何而来?如果打开一个新控制台并运行rails.cache.clear,然后尝试获取关联,会发生什么?我相信缓存已经维护好了…好主意。我们使用memcached来持久化缓存。我试图清除它,但仍然看到返回一个空数组。