[Rails] Split to array then save to DB

Norman Timmler
2006-03-23T17:15:47Z

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 Timmler