# Saving list of IDs in a :string type field?

**URL:** <https://www.ruby-forum.com/t/saving-list-of-ids-in-a-string-type-field/141445>\
**Category:** Rails\
**Created:** [June 25, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/saving-list-of-ids-in-a-string-type-field/141445 "2008-06-25T22:34:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [June 25, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/saving-list-of-ids-in-a-string-type-field/141445/1 "2008-06-25T22:34:38Z")

</div>

Hello,

I’m using a session variable to store a list of IDs during a session.

Basically, a user selects many options from a group of checkboxes, and  
I save that in a session variable.

When I retrieve the session variable and set a model’s variable to  
that value, it works fine.

For instance:

\<input type=“checkbox” name=“user\_profile[language\_ids][]” value=“1” /

> Language 1  
> \<input type=“checkbox” name=“user\_profile[language\_ids][]” value=“2” /  
> Language 2  
> \<input type=“checkbox” name=“user\_profile[language\_ids][]” value=“3” /  
> Language 3  
> \<input type=“checkbox” name=“user\_profile[language\_ids][]” value=“4” /  
> Language 4  
> \<input type=“checkbox” name=“user\_profile[language\_ids][]” value=“5” /  
> Language 5

I set session[:language\_ids] = params[:user\_profile][:language\_ids]

When I retrieve that:  
@user\_profile.language\_ids = session[:language\_ids]  
and I save it, it works fine.

My problem is…I want to store those values in a database, but just  
in 1 field.

So I created a string type field and save it. But the value looks  
strange on the way out of the DB and I can’t set the  
@user\_profile.language\_ids to that value.

Is there a trick to saving this kind of a list of IDs to a DB field?

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [June 26, 2008, 9:44am UTC](https://www.ruby-forum.com/t/saving-list-of-ids-in-a-string-type-field/141445/2 "2008-06-26T09:44:10Z")

</div>

On Jun 25, 9:29 pm, Andy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> So I created a string type field and save it. But the value looks  
> strange on the way out of the DB and I can’t set the  
> @user\_profile.language\_ids to that value.
> 
> Is there a trick to saving this kind of a list of IDs to a DB field?

You;ve got to convert it to a string. Either use serialize as Phillip  
suggested or foo\_ids = session[:language\_ids].join(‘,’) (and then the  
corresponding split whenyou retrieve the row from the database). The  
default to\_s on Array doesn’t do anything very useful in this case.

Fred

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [June 25, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/saving-list-of-ids-in-a-string-type-field/141445/3 "2008-06-25T22:56:20Z")

</div>

On Jun 25, 2008, at 1:29 PM, Andy wrote:

> \<input type=“checkbox” name=“user\_profile[language\_ids][]” value=“5” /
> 
> So I created a string type field and save it. But the value looks  
> strange on the way out of the DB and I can’t set the  
> @user\_profile.language\_ids to that value.
> 
> Is there a trick to saving this kind of a list of IDs to a DB field?

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

serialize(attr\_name, class\_name = Object)

If you have an attribute that needs to be saved to the database as an  
object, and retrieved as the same object, then specify the name of  
that attribute using this method and it will be handled automatically.  
The serialization is done through YAML. If class\_name is specified,  
the serialized object must be of that class on retrieval or  
SerializationTypeMismatch will be raised.  
Attributes

```
 * attr_name - The field name that should be serialized.
 * class_name - Optional, class name that the object type should

```

be equal to.

Example

# Serialize a preferences attribute

class User  
serialize :preferences  
end
