{"id":3108,"date":"2014-03-16T20:28:30","date_gmt":"2014-03-16T20:28:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/16\/undefined-method-to_d-for-0fixnum-when-trying-to-seed-database-collection-of-common-programming-errors\/"},"modified":"2014-03-16T20:28:30","modified_gmt":"2014-03-16T20:28:30","slug":"undefined-method-to_d-for-0fixnum-when-trying-to-seed-database-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/16\/undefined-method-to_d-for-0fixnum-when-trying-to-seed-database-collection-of-common-programming-errors\/","title":{"rendered":"Undefined method `to_d&#39; for 0:Fixnum when trying to seed database?-Collection of common programming errors"},"content":{"rendered":"<p>When I&#8217;m trying to seed my database, I started getting this error:<\/p>\n<pre><code>rake aborted!\nundefined method `to_d' for 0:Fixnum\n<\/code><\/pre>\n<p>This started after I added this method that strips my decimal columns of commas if their present (2,000.00 &lt; 2000.00):<\/p>\n<pre><code>def cost=(num)\n   num.gsub!(',','') if num.is_a?(String)\n   self[:cost] = num.to_d\nend\n<\/code><\/pre>\n<p>I have this same method for each of the columns of my <code>Product<\/code> model:<\/p>\n<pre><code>create_table :products do |t|\n  t.decimal :cost, :precision =&gt; 11, :scale =&gt; 2\n  t.decimal :tax_rate, :precision =&gt; 8, :scale =&gt; 2\n  t.decimal :shipping_cost, :precision =&gt; 8, :scale =&gt; 2\n  t.decimal :cost_per_unit, :precision =&gt; 11, :scale =&gt; 2\n  t.decimal :unit_amount, :precision =&gt; 16\n<\/code><\/pre>\n<p>Now looking at the trace (at https:\/\/gist.github.com\/2265580) I think the problem may be my <code>default_to_zero_if_necessary<\/code> method:<\/p>\n<p>Product.rb<\/p>\n<pre><code>before_validation :default_to_zero_if_necessary, :on =&gt; :create\n\nprivate\n\n  def default_to_zero_if_necessary\n    self.tax_rate = 0 if self.tax_rate.blank?\n    self.shipping_cost = 0 if self.shipping_cost.blank?\n    self.cost = 0 if self.cost.blank?\n    self.cost_per_unit = 0 if self.cost_per_unit.blank?\n    self.unit_amount = 0 if self.unit_amount.blank?\n  end\n<\/code><\/pre>\n<p>Maybe I can&#8217;t use my <code>default_to_zero_if_necessary<\/code> method like this anymore? What&#8217;s really going on here? I&#8217;m not entirely sure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I&#8217;m trying to seed my database, I started getting this error: rake aborted! undefined method `to_d&#8217; for 0:Fixnum This started after I added this method that strips my decimal columns of commas if their present (2,000.00 &lt; 2000.00): def cost=(num) num.gsub!(&#8216;,&#8217;,&#8221;) if num.is_a?(String) self[:cost] = num.to_d end I have this same method for each [&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-3108","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3108","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=3108"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3108\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}