Skip to content

Instantly share code, notes, and snippets.

@ttanimichi
Created September 13, 2014 06:42
Show Gist options
  • Save ttanimichi/59f436486d3b36222dc9 to your computer and use it in GitHub Desktop.
Save ttanimichi/59f436486d3b36222dc9 to your computer and use it in GitHub Desktop.
Railsでcreateやsaveなどをオーバーライドする場合のサンプル。基本的にはbefore_saveなどで代替することが望ましいと思う。また、alias_methodを使って代替することもできる。ref. http://d.hatena.ne.jp/itmammoth/20121129/1354189157
def self.create(*args, &block)
# do something
super(*args, &block)
end
@ttanimichi
Copy link
Author

# コールバックで代替する例
before_create do
  self.code = RandomString.generate(length: 8)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment