0

There might be a group of records in a table where only two fields vary record by record, and rest fields remains same. This calls for a normalization by splitting by a table through a foreign key association. But, in Ruby-on-Rails, it would mean the creation of a model. So, is it still possible to lessen use of disk space?

May be, it is, because it would be reasonable that storing multiple values of one column in a record would require the column to be an array of any type. But declaring the field to be :array type results in an error. So, is there a way to work around it?

1 Answer 1

1

After generating a model, open the model's file. Insert one line for each field.

serialize :field_name

But ensure that the fields for which you are serializing, should be of type

:text

or

:string

If they aren't of such primitive data types, i.e. of another type like

:datetime

then it would return an error.

This step is not complete as a whole. You need to do one complementing step: de-serialize, because in the model-level storage, it is stored as a string starting with "---\n-", which is not suitable for array-type operations.

While reading data from the model, you need to perform the following step:

YAML.load(field_name)

where field_name refers to the field that was serialized.

The above step would return an array, on which you can perform normal array operations.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.