PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/2013/03/slicing-return-object-of-activerecord-base-number-attributes/index.html

https://github.com/zellux/zellux.github.com
HTML | 197 lines | 133 code | 61 blank | 3 comment | 0 complexity | d74cb9671d6ae2b0fe6e4e1bbdaf928c MD5 | raw file
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Slicing Return Object of ActiveRecord::Base#attributes - ZelluX's Dev Blog</title>
  6. <meta name="author" content="zellux">
  7. <meta name="description" content="Slicing Return Object of ActiveRecord::Base#attributes
  8. The problem occurred when I tried to create another object with attributes of current &hellip;">
  9. <!-- http://t.co/dKP3o1e -->
  10. <meta name="HandheldFriendly" content="True">
  11. <meta name="MobileOptimized" content="320">
  12. <meta name="viewport" content="width=device-width, initial-scale=1">
  13. <link href="http://feeds.uri.lv/zellux" rel="alternate" title="ZelluX's Dev Blog" type="application/atom+xml">
  14. <link rel="canonical" href="http://blog.zellux.me/2013/03/slicing-return-object-of-activerecord-base-number-attributes/">
  15. <link href="/favicon.png" rel="shortcut icon">
  16. <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  17. <!--[if lt IE 9]><script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  18. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  19. <link href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,400,700' rel='stylesheet' type='text/css'>
  20. <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
  21. <link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
  22. <link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
  23. <link href="http://fonts.googleapis.com/css?family=Monda" rel="stylesheet" type="text/css">
  24. </head>
  25. <body>
  26. <div class="container">
  27. <div class="left-col">
  28. <div class="intrude-less">
  29. <header id="header" class="inner"><div class="profilepic">
  30. <script src="/javascripts/md5.js"></script>
  31. <script type="text/javascript">
  32. document.write("<img src='http://www.gravatar.com/avatar/" + MD5("zellux@gmail.com") + "?s=160' alt='Profile Picture' style='width: 160px;' />");
  33. </script>
  34. </div>
  35. <h1><a href="/">ZelluX's Dev Blog</a></h1>
  36. <p class="subtitle">Everyday coding notes</p>
  37. <nav id="main-nav"><ul class="main-navigation">
  38. <li><a href="/">Blog</a></li>
  39. <li><a href="/blog/archives">Archives</a></li>
  40. <li><a href="/about">About</a></li>
  41. </ul>
  42. </nav>
  43. <nav id="sub-nav">
  44. <div class="social">
  45. <a class="twitter" href="http://twitter.com/zellux" title="Twitter">Twitter</a>
  46. <a class="github" href="https://github.com/zellux" title="GitHub">GitHub</a>
  47. <a class="rss" href="http://feeds.uri.lv/zellux" title="RSS">RSS</a>
  48. </div>
  49. </nav>
  50. </header>
  51. </div>
  52. </div>
  53. <div class="mid-col">
  54. <div id="banner" class="inner">
  55. <div class="container">
  56. <ul class="feed"></ul>
  57. </div>
  58. <small><a href="http://twitter.com/zellux">zellux</a> @ <a href="http://twitter.com">Twitter</a></small>
  59. <div class="loading">Loading...</div>
  60. </div>
  61. <script src="/javascripts/twitter.js"></script>
  62. <script type="text/javascript">
  63. (function($){
  64. $('#banner').getTwitterFeed('zellux', 5, false);
  65. })(jQuery);
  66. </script>
  67. <div class="mid-col-container">
  68. <div id="content" class="inner"><article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  69. <h1 class="title" itemprop="name">Slicing Return Object of ActiveRecord::Base#attributes</h1>
  70. <div class="entry-content" itemprop="articleBody"><p>The problem occurred when I tried to create another object with attributes of current object like:</p>
  71. <figure class='code'><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
  72. </pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">record</span> <span class="o">=</span> <span class="no">AnotherActiveRecord</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">SomeActiveRecord</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">attributes</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="ss">:user_id</span><span class="p">,</span> <span class="ss">:item_id</span><span class="p">))</span>
  73. </span></code></pre></td></tr></table></div></figure>
  74. <p>As you may find out, <code>Base#attributes</code> here return a Hash with Strings as keys, so when you try to slice them with Symbols, it returns an empty Hash object. To get what you want, you can either use Strings as keys, like</p>
  75. <figure class='code'><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
  76. </pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">record</span> <span class="o">=</span> <span class="no">AnotherActiveRecord</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">SomeActiveRecord</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">attributes</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="s1">&#39;user_id&#39;</span><span class="p">,</span> <span class="s1">&#39;item_id&#39;</span><span class="p">))</span>
  77. </span></code></pre></td></tr></table></div></figure>
  78. <p>or get another Hash object which is indifferent in String and Symbol as key objects:</p>
  79. <figure class='code'><div class='highlight'><table><td class='gutter'><pre class='line-numbers'><span class='line-number'>1</span>
  80. </pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">record</span> <span class="o">=</span> <span class="no">AnotherActiveRecord</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">SomeActiveRecord</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">attributes</span><span class="o">.</span><span class="n">with_indifferent_access</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="ss">:user_id</span><span class="s1">&#39;, :item_id&#39;</span><span class="p">))</span>
  81. </span></code></pre></td></tr></table></div></figure>
  82. </div>
  83. </article>
  84. <div class="share">
  85. <div class="addthis_toolbox addthis_default_style ">
  86. <a class="addthis_button_tweet"></a>
  87. <a class="addthis_button_google_plusone" g:plusone:size="medium"></a>
  88. <a class="addthis_counter addthis_pill_style"></a>
  89. </div>
  90. <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid="></script>
  91. </div>
  92. <section id="comment">
  93. <h1 class="title">Comments</h1>
  94. <div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  95. </div>
  96. </section>
  97. </div>
  98. </div>
  99. <footer id="footer" class="inner"><p>
  100. Copyright &copy; 2013 - zellux -
  101. <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
  102. </p>
  103. Design credit: <a href="http://shashankmehta.in/archive/2012/greyshade.html">Shashank Mehta</a></footer>
  104. <script src="/javascripts/slash.js"></script>
  105. <script src="/javascripts/jquery.fancybox.pack.js"></script>
  106. <script type="text/javascript">
  107. (function($){
  108. $('.fancybox').fancybox();
  109. })(jQuery);
  110. </script> <!-- Delete or comment this line to disable Fancybox -->
  111. <script type="text/javascript">
  112. var disqus_shortname = '50a23bb7';
  113. // var disqus_developer = 1;
  114. var disqus_identifier = 'http://blog.zellux.me/2013/03/slicing-return-object-of-activerecord-base-number-attributes/';
  115. var disqus_url = 'http://blog.zellux.me/2013/03/slicing-return-object-of-activerecord-base-number-attributes/';
  116. var disqus_script = 'embed.js';
  117. (function () {
  118. var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  119. dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
  120. (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  121. }());
  122. </script>
  123. <script type="text/javascript">
  124. var _gaq = _gaq || [];
  125. _gaq.push(['_setAccount', 'UA-25551121-4']);
  126. _gaq.push(['_trackPageview']);
  127. (function() {
  128. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  129. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  130. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  131. })();
  132. </script>
  133. </div>
  134. </div>
  135. </body>
  136. </html>