Forum: Ruby on Rails Split to array then save to DB

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul Livingstone (Guest)
on 2006-03-23 18:33
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.
Norman T. (Guest)
on 2006-03-23 19:16
(Received via mailing list)
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
Paul Livingstone (Guest)
on 2006-03-23 19:17
Thanks Norman, most helpful.
John B. (Guest)
on 2006-03-23 19:36
(Received via mailing list)
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.
This topic is locked and can not be replied to.