Skip to content

Instantly share code, notes, and snippets.

@derencius
Created April 10, 2011 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derencius/912042 to your computer and use it in GitHub Desktop.
Save derencius/912042 to your computer and use it in GitHub Desktop.
Using model's sequence next value before inserting for special manipulation.
# id : serial
# check_digit : integer
class Client
set_primary_key :id
before_create :define_check_digit
private
def self.next_id
Integer(connection.select_value "SELECT nextval('#{sequence_name}')")
end
def define_check_digit
self.id = self.class.next_id
self.check_digit = Digit.calc id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment