# Converting Array to String in a model using before filters

**URL:** <https://www.ruby-forum.com/t/converting-array-to-string-in-a-model-using-before-filters/163348>\
**Category:** Rails\
**Created:** [March 19, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/converting-array-to-string-in-a-model-using-before-filters/163348 "2009-03-19T14:17:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/converting-array-to-string-in-a-model-using-before-filters/163348/1 "2009-03-19T14:17:09Z")

</div>

Hi,

```
I have a list coming in a field like :selected_lists => [1,2,3,4]

```

from a form  
while storing it in a database i need to join this list into a  
string

so then in my model class,

* * *

1. Added a filter before\_save :convert\_array\_to\_string
2. Added an attr\_accessor :selected\_lists

* * *

## def convert\_array\_to\_string self.lists\_selected = self.lists\_selected.join(",") puts self.lists\_selected return true end

so that before saving i call a function to format the array to  
string and store it using attr\_accessor method  
I printed the “self.lists\_selected” and i got “1,2,3,4” as the  
result

I thought this would do my job  
I am wondering why ‘nil’ is stored in the table.  
Please help me out off this guys.

Regards,  
Vimal Das

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/converting-array-to-string-in-a-model-using-before-filters/163348/2 "2009-03-19T14:20:06Z")

</div>

> ```
> I have a list coming in a field like :selected_lists => [1,2,3,4]
> 
> ```
> 
> from a form  
> Correction :selected\_lists =\> [1,2,3,4] to ---------\> :lists\_selected  
> =\> [1,2,3,4]  
> Regards,  
> Vimal Das

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/converting-array-to-string-in-a-model-using-before-filters/163348/3 "2009-03-19T15:01:43Z")

</div>

So, is there anyother way to carry this out of the box

Regards,  
Vimal Das

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/converting-array-to-string-in-a-model-using-before-filters/163348/4 "2009-03-19T14:58:47Z")

</div>

On 19 Mar 2009, at 13:16, vimal wrote:

> * * *
> 
> so that before saving i call a function to format the array to  
> string and store it using attr\_accessor method  
> I printed the “self.lists\_selected” and i got “1,2,3,4” as the  
> result
> 
> I thought this would do my job  
> I am wondering why ‘nil’ is stored in the table.  
> Please help me out off this guys.

Because your attr\_accessor overwrote the accessor which saves to a  
database column with one that saves to an instance variable

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 20, 2009, 7:18am UTC](https://www.ruby-forum.com/t/converting-array-to-string-in-a-model-using-before-filters/163348/5 "2009-03-20T07:18:58Z")

</div>

Thanks again Fred to make me try this out…  
I got it after analysing ur reply further

So the trick is not to add a attr\_accessor for lists\_selected

Regards,  
Vimal Das

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 20, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/converting-array-to-string-in-a-model-using-before-filters/163348/6 "2009-03-20T14:22:55Z")

</div>

vimal wrote:

> So, is there anyother way to carry this out of the box

I’m not sure precisely what you’re trying to accomplish here, but from  
what I can tell it would probably be a lot simpler and more flexible to  
use the built-in serialization provided by ActiveRecord:

* * *

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.

Parameters:  
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

Using this technique you can save you Array object to a string field and  
get back an Array object automatically. All you have to do is add the  
serialize line in your model as shown above.
