{"id":7043,"date":"2014-05-17T00:24:21","date_gmt":"2014-05-17T00:24:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/marshal-not-serializing-or-deserializing-nested-serialized-attributes-properly-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:24:21","modified_gmt":"2014-05-17T00:24:21","slug":"marshal-not-serializing-or-deserializing-nested-serialized-attributes-properly-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/marshal-not-serializing-or-deserializing-nested-serialized-attributes-properly-collection-of-common-programming-errors\/","title":{"rendered":"Marshal not serializing or deserializing nested serialized attributes properly-Collection of common programming errors"},"content":{"rendered":"<p>This is kind of a tough problem to explain, because I don&#8217;t really know what is going on. I&#8217;ve made a repository with instructions of how to reproduce:<\/p>\n<p>https:\/\/github.com\/bricker\/cache_nested_serialized_attributes<\/p>\n<p>Basically:<\/p>\n<ul>\n<li>User has_many Posts<\/li>\n<li>Post has serialized attribute :metadata<\/li>\n<\/ul>\n<p>And to reproduce:<\/p>\n<pre><code>users = User.first(2)\nusers.first.posts.first\nusers.last.posts.first\ndump = Marshal.dump(users)\nMarshal.load(dump)\n=&gt; [#, \n    :@new_record]\n<\/code><\/pre>\n<p>You can see the unexpected output in that final line. It only seems to occur under those specific conditions. Just calling <code>users.first.posts<\/code> works fine. Not calling <code>.posts<\/code> at all works fine. It&#8217;s only when I load a specific post from a user before dumping that this happens.<\/p>\n<p>The main difference I notice between Rails 4 (which works properly) and Rails 3.2.13 (which doesn&#8217;t work) is that when calling <code>Marshal.dump(users)<\/code> in Rails 3, the Posts are reloaded:<\/p>\n<pre><code>dump = Marshal.dump(users)\n  Post Load (0.2ms)  SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1\n  Post Load (0.2ms)  SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 2\n<\/code><\/pre>\n<p>&#8230; which doesn&#8217;t happen in Rails 4.<\/p>\n<p>I wish I could explain better but it&#8217;s such an obscure problem. Please ask questions if you need clarification, and look at or clone the repo.<\/p>\n<p>This is tested and confirmed on a vanilla Rails 3.2.13 (see repository). This behavior does <em>not<\/em> happen in Rails 4.<\/p>\n<p>Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is kind of a tough problem to explain, because I don&#8217;t really know what is going on. I&#8217;ve made a repository with instructions of how to reproduce: https:\/\/github.com\/bricker\/cache_nested_serialized_attributes Basically: User has_many Posts Post has serialized attribute :metadata And to reproduce: users = User.first(2) users.first.posts.first users.last.posts.first dump = Marshal.dump(users) Marshal.load(dump) =&gt; [#, :@new_record] You can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7043","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7043"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7043\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}