Skip to content

Instantly share code, notes, and snippets.

@elhoyos
Created November 13, 2013 15:40
Show Gist options
  • Select an option

  • Save elhoyos/7451129 to your computer and use it in GitHub Desktop.

Select an option

Save elhoyos/7451129 to your computer and use it in GitHub Desktop.
Initialize default values in Rails
# 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