Joined May 2011
·

Boris Barroso

bonsaiERP
·
La Paz, Bolivia
·
·
·

def assign_organization
  if self.new_record? && self.organization.nil?
    organization = Organization.new
    organization.save! # Raises exception on error
    self.organization = organization
  end
  rescue
   nil
end

better do this if you have has_one :organisation

def assign_organization
  if new_record? && organization.blank?
    self.build_organisation
  end
end

You can also define the default value with:
ruby add_column :users, :emails, :string, array: true, default: []
no need to pass a String.

Posted to Using delegate in Rails over 1 year ago

You can also allow_nil: true which is really helpful so it doesn't raise exceptions if the delegated object returns nil

Achievements
158 Karma
9,852 Total ProTip Views