create_account_programmatically.rdoc

doc/guides/create_account_programmatically.rdoc
Last Update: 2020-06-30 15:06:09 -0700

Create an account record programmatically

In some scenarios you might want to create an account records programmatically, for example in your tests.

If you’re storing passwords in a separate table, you can create an account records as follows:

account_id = DB[:accounts].insert(
  email:     "name@example.com",
  status_id: 2, # verified
)

DB[:account_password_hashes].insert(
  id:            account_id,
  password_hash: BCrypt::Password.create("secret").to_s,
)

If the password is stored in a column in the accounts table:

account_id = DB[:accounts].insert(
  email:         "name@example.com",
  password_hash: BCrypt::Password.create("secret").to_s,
  status_id:     2, # verified
)

If you are creating accounts in your tests, you probably want to use the :cost option, otherwise you will have very slow tests:

account_id = DB[:accounts].insert(
  email:     "name@example.com",
  status_id: 2, # verified
)

DB[:account_password_hashes].insert(
  id:            account_id,
  password_hash: BCrypt::Password.create("secret", cost: BCrypt::Engine::MIN_COST).to_s,
)