Skip to content

Instantly share code, notes, and snippets.

@fukajun
Created February 5, 2016 02:06
Show Gist options
  • Save fukajun/8d476965cf11a184bcec to your computer and use it in GitHub Desktop.
Save fukajun/8d476965cf11a184bcec to your computer and use it in GitHub Desktop.
ValueObject
class User < ActinveRecord::Base
# address = '1540022 tokyo aa street
Address = Struct.new(:zip, :street)
before_save :rebuild_address
def address_object
@address_object ||= Address.new(address[0..5], address[6..-1])
end
def rebuild_address
address = @address_object.zip + ...
end
end
hoge.address_zip
hoge.address_streeet
hoge.address.zip
hoge.address.streeet
hoge.address_objct.zip = '12345678'
hoge.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment