title: Ruby on Rails JSON serialization
body: <p>I'm a complete newcomer to Ruby on Rails so please forgive me if this is an obvious question.</p>
  
  <p>I'm returning a JSON object from a controller method (let's say the class name is "foo" and it has a property "bar").</p>
  
  <p>I'd expected this to serialize as:</p>
  
  <pre><code>{"bar" : "barValue" }
  </code></pre>
  
  <p>However, it seems to serialize as</p>
  
  <pre><code>{"foo" : {"bar" : "barValue"}}
  </code></pre>
  
  <p>This seems out of joint with a.) what other languages do , b.) (More importantly) what javascript does.</p>
  
  <p>Say I've defined the same class foo in Javascript:</p>
  
  <pre><code>var fooInstance = new Foo();
  fooInstance.bar = "barValue";
  </code></pre>
  
  <p>And I then stringify that using one of a Javascript JSON library (e.g. <a href="https://github.com/douglascrockford/JSON-js" rel="nofollow">https://github.com/douglascrockford/JSON-js</a> ). Then the output is something along the lines of:</p>
  
  <pre><code>{"bar" : "barValue" }
  </code></pre>
  
  <p>But inputs (as well as outputs) to my controller methods expect:</p>
  
  <pre><code>{"foo" : {"bar" : "barValue"}
  </code></pre>
  
  <p>So I have to write code along these lines to make it work:</p>
  
  <pre><code>var fooInstance = new Foo();
  fooInstance.bar = "barValue";
  var dummyObjectToKeepRailsHappy = { foo : fooInstance};
  </code></pre>
  
  <p>So- am I doing Rails serialization incorrectly? Or is there a reason it works this way?</p>