I'm pretty sure what I'm looking for is proper usage of virtual attributes?
Regardless.
Here is an overview of my ActiveRecord in-so-far:
class User < ActiveRecord::Base
validates :email, uniqueness: { case_sensitive: false }, email: true
validates :username, uniqueness: { case_sensitive: false }
validates :password, confirmation: true
validates :password_confirmation, presence: true
end
And the schema associated:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |u|
u.string :email
u.string :username
u.string :password_hash
u.string :salt
u.timestamps
end
end
end
My goal is to be able to initialize a new record with (something like) the following:
User.new({"email"=>"emailaddresshere",
"username"=>"usernamehere",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"})
I need a way to, after all the lovely validation checks, but before attempting to create a new DB record, transform the password into the password_hash, as well as generate a salt. I know HOW to acquire the hash and salt, I just don't know how I would be able to interject a snippet of code into the record creation process, to modify the parameters provided. I am also unsure of how to make sure that it all works without throwing an UnknownAttributeError.
Any way to do this? I could obviously write a "def register" method which manually transforms everything into a new record, but then it would render the password and password_confirmation validations useless, as the record will have been transformed by the time it hits User.new().