Split to array then save to DB


#1

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.


#2

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


#3

Thanks Norman, most helpful.


#4

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.