I have a csv file and in my controller: uploaded_file = params[:upload][:csv].read @rows << FasterCSV.parse(uploaded_file) I want to put in @rows only the first 10 lines but the read method reads all the file. How can I do? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 March 2011 18:52, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a csv file and in my controller: > > uploaded_file = params[:upload][:csv].read > @rows << FasterCSV.parse(uploaded_file) > > I want to put in @rows only the first 10 lines but the read method > reads all the file.I think you can do something like FasterCSV.foreach( uploaded_file) do |row| # do something for first 10 rows then break out end Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 March 2011 22:06, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 26 March 2011 18:52, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I have a csv file and in my controller: >> >> uploaded_file = params[:upload][:csv].read >> @rows << FasterCSV.parse(uploaded_file) >> >> I want to put in @rows only the first 10 lines but the read method >> reads all the file. > > I think you can do something like > FasterCSV.foreach( uploaded_file) do |row| > # do something for first 10 rows then break out > endI''ve solved with: uploaded_file = params[:upload][:csv].read FasterCSV.parse(uploaded_file) do |row| @rows[i] = row i += 1 break row if i == 10 end> > Colin > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.