Note that there are some explanatory texts on larger screens.

plurals
  1. POParameters not getting saved in Rails 3 app
    primarykey
    data
    text
    <p>I am a Rails 3 beginner working on an application that allows the user to enter monetary values. I am using a jQuery plugin (<a href="http://github.com/plentz/jquery-maskmoney" rel="nofollow noreferrer">http://github.com/plentz/jquery-maskmoney</a>) to display decimal values as monetary values on the edit page. All the decimal attributes are manipulated as currency when editing. When saving the, purchase_price and capital_reserves are saved correctly. The Property before_save function is called and the currency values ($123.45) get converted to decimal values (123.45).</p> <p>The problem is that the associated rent values are never saved if I just edit the rent prices. I can see the correct values being sent in the parameters but the before_save code in the Rent model is never triggered. If I edit the apartment number value and a rent price, then the rent price is saved correctly. Also, after that I can just edit the price for the apartment number I previously modified. However, any other rent price will not be updated.</p> <p>I am using MySQL and Rails 3.0.9</p> <p>Steps to reproduce: Non-Issue</p> <ol> <li>Edit a property</li> <li>Modify the property's purchase price and/or capital reserves value</li> <li>Click Update</li> <li>These values are converted from currency values ($1,234.56) to decimal (1234.56) by the before_save code in Property</li> </ol> <p>Issue</p> <ol> <li>Edit a property</li> <li>Modify the property's rent current price and/or market price values</li> <li>Click Update</li> <li>These values do not get saved. The before_save code is not called in the Rent model.</li> </ol> <p>Issue</p> <ol> <li>Edit a property</li> <li>Modify the property's purchase price and/or capital reserves value, but also edit the apartment number</li> <li>Click Update</li> <li>These values are saved correctly.</li> <li>Now you can edit the price values for the row previously saved and those prices are saved. Why?</li> </ol> <p>I made a small project to showcase this if anyone is interested (<a href="https://github.com/michaelklem/Money-Test" rel="nofollow noreferrer">https://github.com/michaelklem/Money-Test</a>).</p> <p>Here are my data models.</p> <p><strong>Property</strong> class</p> <pre><code>class Property &lt; ActiveRecord::Base before_save :handle_before_save has_many :rents, :dependent =&gt; :destroy accepts_nested_attributes_for :rents, :allow_destroy =&gt; true def handle_before_save if new_record? generate_default_rent_data end remove_currency_formatting end def generate_default_rent_data 10.times do |i| self.rents.build(:apartment_number =&gt; i+1) end end def remove_currency_formatting if self.capital_reserves.to_s != self.capital_reserves_before_type_cast.to_s self.capital_reserves = Property.remove_currency_format(self.capital_reserves_before_type_cast) end if self.purchase_price.to_s != self.purchase_price_before_type_cast.to_s self.purchase_price = Property.remove_currency_format(self.purchase_price_before_type_cast) end end # # handles removing all characters from currency objects # except for 0-9 and . # def self.remove_currency_format(currency_attribute) currency_attribute.gsub(/[^0-9.]/, "") end end </code></pre> <p><strong>Rent</strong> class:</p> <pre><code>class Rent &lt; ActiveRecord::Base belongs_to :property before_save :handle_before_save def handle_before_save remove_currency_formatting end def remove_currency_formatting if self.current_price.to_s != self.current_price_before_type_cast.to_s self.current_price = Property.remove_currency_format(self.current_price_before_type_cast) end if self.market_price.to_s != self.market_price_before_type_cast.to_s self.market_price = Property.remove_currency_format(self.market_price_before_type_cast) end end end </code></pre> <p>Not sure if I am seeing a bug or missing something obvious. Thanks for looking into this.</p> <p><strong>Update</strong></p> <p>After I posted this I found this SO question <a href="https://stackoverflow.com/questions/3596105/stripping-the-first-character-of-a-string">Stripping the first character of a string</a> that helped me figure this out. It still seems to me that my original issue is a bug.</p> <p>I was able to simplify my code to the following and everything works.</p> <pre><code>class Property &lt; ActiveRecord::Base before_save :handle_before_save has_many :rents, :dependent =&gt; :destroy accepts_nested_attributes_for :rents, :allow_destroy =&gt; true def handle_before_save if new_record? generate_default_rent_data end end def purchase_price=(data) if data.is_a?(String) data = Property.remove_currency_format(data) write_attribute(:purchase_price, data) end end def capital_reserves=(data) if data.is_a?(String) data = Property.remove_currency_format(data) write_attribute(:capital_reserves, data) end end # # generate some default data # def generate_default_rent_data 10.times do |i| self.rents.build(:apartment_number =&gt; i+1) # provide a default value for apartment numbers end end def self.remove_currency_formatting(data) if data.is_a?(String) data = Property.remove_currency_format(data) end return data end # # handles removing all characters from currency objects # except for 0-9 and . # def self.remove_currency_format(currency_attribute) currency_attribute.gsub(/[^0-9.]/, "") end def purchase_price=(data) _write_attribute(:purchase_price, data) end def capital_reserves=(data) _write_attribute(:capital_reserves, data) end private def _write_attribute(attribute, data) write_attribute(attribute, Property.remove_currency_formatting(data)) end end class Rent &lt; ActiveRecord::Base belongs_to :property def current_price=(data) _write_attribute(:current_price, data) end def market_price=(data) _write_attribute(:market_price, data) end private def _write_attribute(attribute, data) write_attribute(attribute, Property.remove_currency_formatting(data)) end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload