<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[My Little Diary]]></title>
  <link href="http://ahmadsherif.github.com/atom.xml" rel="self"/>
  <link href="http://ahmadsherif.github.com/"/>
  <updated>2013-01-30T16:18:33+02:00</updated>
  <id>http://ahmadsherif.github.com/</id>
  <author>
    <name><![CDATA[Ahmad Sherif]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Mongoid default fields can give you hard time]]></title>
    <link href="http://ahmadsherif.github.com/blog/2013/01/29/mongoid-default-fields-can-give-you-hard-time/"/>
    <updated>2013-01-29T22:56:00+02:00</updated>
    <id>http://ahmadsherif.github.com/blog/2013/01/29/mongoid-default-fields-can-give-you-hard-time</id>
    <content type="html"><![CDATA[<p>So I was coding peacefully when our product owner was doing some tests on
the new features we implemented over that past two weeks. He pulls,
bundles, runs the server and goes to the index route only to find the cheerful
an empty page. &#8220;Where are my previous records?&#8221; he might have thought, &#8220;I didn&#8217;t drop the db&#8221;.
He&#8217;s of a good technical expierence so he fires up Rails console to inspect this issue; one command, press
Enter, and Voila, the documents are there!</p>

<!-- more -->


<p>He decides to call me to tell me that I&#8217;m a lousy developer that doesn&#8217;t
deserve his salary and to fix this issue ASAP so he can deserve his
salary too :).</p>

<p>So what do we have in here? A model named <code>Post</code> (not a real example,
but the point is the same) with a pretty straight-forward lines:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Post</span>
</span><span class='line'>  <span class="kp">include</span> <span class="ss">Mongoid</span><span class="p">:</span><span class="ss">:Document</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">field</span> <span class="ss">:title</span>
</span><span class='line'>  <span class="n">field</span> <span class="ss">:content</span>
</span><span class='line'>  <span class="n">field</span> <span class="ss">:published</span><span class="p">,</span> <span class="ss">type</span><span class="p">:</span> <span class="no">Boolean</span><span class="p">,</span> <span class="ss">default</span><span class="p">:</span> <span class="kp">true</span>
</span><span class='line'>  <span class="n">field</span> <span class="ss">:visits_count</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">scope</span> <span class="ss">:most_visited</span><span class="p">,</span> <span class="no">Proc</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="n">where</span><span class="p">(</span><span class="ss">published</span><span class="p">:</span> <span class="kp">true</span><span class="p">)</span><span class="o">.</span><span class="n">desc</span><span class="p">(</span><span class="ss">:visits_count</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The controller is no different too:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">PostsController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">index</span>
</span><span class='line'>    <span class="vi">@posts</span> <span class="o">=</span> <span class="no">Post</span><span class="o">.</span><span class="n">most_visited</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>So what&#8217;s the problem? I don&#8217;t trust anyone by default when it comes to
code, so I switch to the console and see for my self:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>1.9.3p327 :001 &gt; Post.most_visited.to_a
</span><span class='line'><span class="o">=</span>&gt; <span class="o">[]</span>
</span><span class='line'>1.9.3p327 :002 &gt; Post.all.to_a
</span><span class='line'><span class="o">=</span>&gt; <span class="o">[</span><span class="c">#&lt;Post _id: 50f81a92c12d4d6b35000002, _type: nil, title: &#39;blah&#39;, content: &#39;another blah&#39;, published: true, visits_count: 5&gt;, #&lt;Post _id: 50f81a92c12d4d6b35000003, _type: nil, title: &#39;meh&#39;, content: &#39;another meh&#39;, published: true, visits_count: 3&gt;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>OK, he wasn&#8217;t lying about it, but what the X? The scope is only using
one condition (<code>published: true</code>) and <code>Post.all</code> says that all records has
<code>published</code> set to true. After debugging and checking everything that
could cause such weirdness, he decided that it&#8217;s a waste of time to continue and
dropping database and starting from scratch would be the solution for
now. As a shot in the dark, I decided to check Mongo itself, maybe it
can tell us what on Earth is going on.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>mongo
</span><span class='line'>&gt; use blog_development
</span><span class='line'>&gt; db.posts.findOne<span class="o">()</span>
</span><span class='line'><span class="o">{</span>
</span><span class='line'>        <span class="s2">&quot;_id&quot;</span> : ObjectId<span class="o">(</span><span class="s2">&quot;50f81a92c12d4d6b35000003&quot;</span><span class="o">)</span>,
</span><span class='line'>        <span class="s2">&quot;title&quot;</span> : <span class="s2">&quot;blah&quot;</span>,
</span><span class='line'>        <span class="s2">&quot;content&quot;</span> : <span class="s2">&quot;another blah&quot;</span>,
</span><span class='line'>        <span class="s2">&quot;visits_count&quot;</span> : 5
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>See the issue? There&#8217;s no <code>published</code> propery for the document! Then
what are those documents we got from Rails console?</p>

<p>Turns out the problem was that I added <code>published</code> field to the model
after those documents were already persisted, Mongoid was just filling
in <code>published</code> because it got it in the model, set it to true because I
told it to. We were telling Mongo to filter by <code>published</code> but it
doesn&#8217;t find any <code>published</code> properties on the documents so it returns
nothing. Pretty simple but Mongoid made us take it for granted
that <code>published</code> was there, Damn you Mongoid, this isn&#8217;t the first time!</p>

<p>The rest is easy, calling save on the documents made everything work
just fine (a quick-n-dirty solution, running <code>update_all</code> should be the
way), but we learned a valuable lesson, you don&#8217;t mess with fields with
default value without making a Rake task to populate them, or you run in
circles while the solution was just there, hidden in plain sight.</p>

<p>PS: We&#8217;re good friends, me and the product owner, I&#8217;m just being
sarcastic here :D.</p>
]]></content>
  </entry>
  
</feed>
