Created
November 13, 2013 15:40
-
-
Save elhoyos/7451129 to your computer and use it in GitHub Desktop.
Initialize default values in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # From: http://stackoverflow.com/a/5127684/638425 | |
| # WARNING: See the caveats. | |
| class Person | |
| has_one :address | |
| after_initialize :init | |
| def init | |
| self.number ||= 0.0 #will set the default value only if it's nil | |
| self.address ||= build_address #let's you set a default association | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment