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

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. :slight_smile:

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. :slight_smile:

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. :slight_smile:

That has become obvious. :smiley:
Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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.

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

… specifically … _before_type_cast

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
[email protected]