How to setup default attributes in a ruby model

Posted by webdestroya on Stack Overflow See other posts from Stack Overflow or by webdestroya
Published on 2010-05-22T21:35:52Z Indexed on 2010/05/22 21:40 UTC
Read the original article Hit count: 409

Filed under:
|
|

I have a model User and when I create one, I want to pragmatically setup some API keys and what not, specifically:
@user.apikey = Digest::MD5.hexdigest(BCrypt::Password.create("jibberish").to_s)

I want to be able to run User.create!(:email=>"[email protected]") and have it create a user with a randomly generated API key, and secret.

I currently am doing this in the controller, but when I tried to add a default user to the seeds.rb file, I am getting an SQL error (saying my apikey is null).

I tried overriding the save definition, but that seemed to cause problems when I updated the model, because it would override the values. I tried overriding the initialize definition, but that is returning a nil:NilClass and breaking things.

Is there a better way to do this?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby