# How to "encrypt" a string to an integer in a model?

**URL:** <https://www.ruby-forum.com/t/how-to-encrypt-a-string-to-an-integer-in-a-model/179547>\
**Category:** Rails\
**Created:** [November 27, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/how-to-encrypt-a-string-to-an-integer-in-a-model/179547 "2009-11-27T23:36:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 27, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/how-to-encrypt-a-string-to-an-integer-in-a-model/179547/1 "2009-11-27T23:36:41Z")

</div>

Newb here.

I want to create a model/form in which a user enters a string, the  
string gets converted to an integer.

The string/integer conversion is reversible. Don’t ask how… it just  
is.

I want to store the result as a an integer into the column of the  
database.

So … I have  
t.integer :reversible , :null =\> false

When I attempt to validate the string (it has to be of a certain form)  
… all I see is a digit zero.

What’s the right way to do this?

The more general case is to have several fields on a form that you want  
to validate/process and then have that result go into a single column.  
What’s a way to do that via Rails?

Remember … I’m a newb. 🙂

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 28, 2009, 2:03am UTC](https://www.ruby-forum.com/t/how-to-encrypt-a-string-to-an-integer-in-a-model/179547/2 "2009-11-28T02:03:26Z")

</div>

Ralph S. wrote:

> Newb here.
> 
> I want to create a model/form in which a user enters a string, the  
> string gets converted to an integer.
> 
> The string/integer conversion is reversible.

Then why not just store the string in the DB?

[…]

> What’s the right way to do this?

Don’t try. This is a hare-brained idea. 🙂

> The more general case is to have several fields on a form that you want  
> to validate/process and then have that result go into a single column.  
> What’s a way to do that via Rails?

You want AR::Base.serialize. However, this is seldom a good idea. It’s  
usually better to design a proper schema.

> Remember … I’m a newb. 🙂

## That has become obvious. 😃 Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 28, 2009, 2:43am UTC](https://www.ruby-forum.com/t/how-to-encrypt-a-string-to-an-integer-in-a-model/179547/3 "2009-11-28T02:43:12Z")

</div>

> You want AR::Base.serialize. However, this is seldom a good idea. It’s  
> usually better to design a proper schema.

No, I don’t want to serialize.

What I want to do is put up a field on the form that is not related to a  
column in a database. I want to validate the response to this field  
(and not save it in the database), possibly modify other fields on the  
form that then go into the database.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 28, 2009, 2:54am UTC](https://www.ruby-forum.com/t/how-to-encrypt-a-string-to-an-integer-in-a-model/179547/4 "2009-11-28T02:54:09Z")

</div>

Ralph S. wrote:

> > You want AR::Base.serialize. However, this is seldom a good idea. It’s  
> > usually better to design a proper schema.
> 
> No, I don’t want to serialize.
> 
> What I want to do is put up a field on the form that is not related to a  
> column in a database. I want to validate the response to this field  
> (and not save it in the database), possibly modify other fields on the  
> form that then go into the database.

I think part of the answer is in

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

… specifically … \_before\_type\_cast

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 28, 2009, 3:11am UTC](https://www.ruby-forum.com/t/how-to-encrypt-a-string-to-an-integer-in-a-model/179547/5 "2009-11-28T03:11:35Z")

</div>

Ralph S. wrote:

> > You want AR::Base.serialize. However, this is seldom a good idea. It’s  
> > usually better to design a proper schema.
> 
> No, I don’t want to serialize.
> 
> What I want to do is put up a field on the form that is not related to a  
> column in a database. I want to validate the response to this field  
> (and not save it in the database), possibly modify other fields on the  
> form that then go into the database.

You can do this quite easily – remember that you can use attr\_accessor  
on AR objects just as you can on any other objects.

For the rest, read about AR validations and callbacks.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
