# Split to array then save to DB

**URL:** <https://www.ruby-forum.com/t/split-to-array-then-save-to-db/55761>\
**Category:** Rails\
**Created:** [March 23, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/split-to-array-then-save-to-db/55761 "2006-03-23T17:33:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Livingstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7c8e57/32.png) [@Paul\_Livingstone](https://www.ruby-forum.com/u/Paul_Livingstone)\
**Post date:** [March 23, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/split-to-array-then-save-to-db/55761/1 "2006-03-23T17:33:17Z")

</div>

Basically I want to take a collection of words like “apple orange  
banana” textfield in a form and store them into the DB as individual  
rows.

1. apple
2. orange
3. banana

I know I need to use the .split method to divide the words into an  
array, but i’m not sure how to then save them and where this should  
happen (in the controller or the model).

I’m a newb at this, so be gentle.

---

<div class="post-metadata">

**Author:** ![Paul\_Livingstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7c8e57/32.png) [@Paul\_Livingstone](https://www.ruby-forum.com/u/Paul_Livingstone)\
**Post date:** [March 23, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/split-to-array-then-save-to-db/55761/2 "2006-03-23T18:16:00Z")

</div>

Am Donnerstag, den 23.03.2006, 17:33 +0100 schrieb Paul Livingstone:

> Basically I want to take a collection of words like “apple orange  
> banana” textfield in a form and store them into the DB as individual  
> rows.
> 
> 1. apple
> 2. orange
> 3. banana

class Fruit \< ActiveRecord::Base  
class \< self  
def create\_multiple\_from\_string(string)  
names = string.split(/\s/)  
names.each { |name| create(:name =\> name) }  
end  
end  
end

> I know I need to use the .split method to divide the words into an  
> array, but i’m not sure how to then save them and where this should  
> happen (in the controller or the model).

If it is a common task, you want to invoke from several controllers, you  
should define a model class method. If not, you could implement it in  
the controller.

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![Paul\_Livingstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7c8e57/32.png) [@Paul\_Livingstone](https://www.ruby-forum.com/u/Paul_Livingstone)\
**Post date:** [March 23, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/split-to-array-then-save-to-db/55761/3 "2006-03-23T18:17:47Z")

</div>

Thanks Norman, most helpful.

---

<div class="post-metadata">

**Author:** ![Paul\_Livingstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7c8e57/32.png) [@Paul\_Livingstone](https://www.ruby-forum.com/u/Paul_Livingstone)\
**Post date:** [March 23, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/split-to-array-then-save-to-db/55761/4 "2006-03-23T18:36:48Z")

</div>

Apologies if posting here is not appropriate - Rails job in San Diego  
working on an electronic publishing / content management application  
[c.f. apache cocoon but with editor] - hit me up directly if interested.  
Position is full time permanent, and is on-site.

Thanks.
