For some reason @store object with three attributes won’t assign the third value(generated token string).The model has user_id, product_id and token_string.But when it comes to token_generate function nothing is assigned,and end up with nil in the token_string column in the database .Product_id and user_id are saved perfectly in the database though.What is going on here?Thank you in advance.
class Store < ActiveRecord::Base
require 'digest/sha1'
attr_accessor :token_string
before_save :token_generate
def save_with_payment
#Until here the object has user_id and product_id attribute values
save!
end
private
def token_generate
self.token_string = Digest::SHA1.hexdigest("random string")
end
end
controller
def create
@store=Store.new(params[:store])
if @store.save_with_payment
redirect_to :controller=>"products",:action=>"index"
else
redirect_to :action=>"new"
end
end
@SrdjanPejic is correct, try removing the
attr_accessor :token_stringline which is likely blocking setting the :attributes hash value needed for the INSERT statement.