<bill-4qJMilvFZdi/7u6bA4Lga2Xnswh1EIUO@public.gmane.org>
2005-Aug-30 02:13 UTC
array data to table?
Can someone help a newbie here. Trying to learn Ruby and rails at the same time is troublesome. I tried looking for this in the pick axe and rails books and could find an answer to my problem. I have an array called @labelArr that has n number of elements and I want to add each element of the array to a table called Labels that has two fields (id & label) can someone help me with how I would do this? thanks, -bill
<bill-4qJMilvFZdi/7u6bA4Lga2Xnswh1EIUO@public.gmane.org>
2005-Aug-30 03:58 UTC
array data to table?
Can someone help a newbie here. Trying to learn Ruby and rails at the same time is troublesome. I tried looking for this in the pick axe and rails books and could find an answer to my problem. Can someone help a newbie here? I have an array called @labelArr that has n number of elements and I want to add each element of the array to a table called Labels that has two fields (id & labelName) can someone help me with how I would do this? thanks, -bill
bill-4qJMilvFZdi/7u6bA4Lga2Xnswh1EIUO@public.gmane.org wrote:> Can someone help a newbie here. Trying to learn Ruby and rails at the same > time is troublesome. I tried looking for this in the pick axe and rails books > and could find an answer to my problem. > > I have an array called @labelArr that has n number of elements and I want to > add each element of the array to a table called Labels that has two fields (id & > label) can someone help me with how I would do this?If == app/models/label.rb class Label < ActiveRecord::Base end = then the code you want is: @labelArr.each do |element| Label.new( :label => element ).save end There''s probably a more terse way of doing it, but that should be ok. (cc''d direct because the mailing list''s being *really* slow). -- Alex
You could solve it with something like this one-liner:
@labelArr.each {|label| Label.create(:label => label) }
//samuel
bill-4qJMilvFZdi/7u6bA4Lga2Xnswh1EIUO@public.gmane.org
wrote:> Can someone help a newbie here. Trying to learn Ruby and rails at the same
> time is troublesome. I tried looking for this in the pick axe and rails
books
> and could find an answer to my problem.
>
> I have an array called @labelArr that has n number of elements and I want
to
> add each element of the array to a table called Labels that has two fields
(id &
> label) can someone help me with how I would do this?
>
>
> thanks,
>
> -bill
>
>
>
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>