Processing data

17 views
Skip to first unread message

Rynn Steinbolt

unread,
Jun 3, 2016, 3:17:25 PM6/3/16
to rubyonra...@googlegroups.com
Hello Folks!

Im stuck with my current task, partly because there seems to be a lack
of basic understanding of syntax i cant pinpoint. I hope you can show me
a way out!

What Im trying to do is processing one model's data, thus turning it
into another model's data. It works with one datum, but I cant get it to
work with the whole data set. I tried several versions of code in
several places, but this is the current version:

class RawDatum < ActiveRecord::Base
default_scope :order => 'raw_data.timestamp DESC'
def self.process_raw_data
i = 0
RawDatum.find_each do |raw|
data[i] = ProcessedDatum.create
data[i].period_label = raw.status
i += 1
end
return data
end
end

class ProcessedDatum < ActiveRecord::Base
end

class ProcessedDataController < ApplicationController
def index
@data = RawDatum.process_raw_data
end
end

when i open localhost:3000/processed_data/, i get the following error:
undefined local variable or method `data' for #<Class:0x9c785f4>
app/models/raw_datum.rb:11:in `block in process_raw_data'
app/models/raw_datum.rb:10:in `process_raw_data'
app/controllers/processed_data_controller.rb:9:in `index'

the data variable is supposed to be an array that should hold
ProcessedDatum-objects, its just a container variable. it seems i have
to define it, but how would I do that? am i doing this entirely wrong,
and if so, what would be a better way?

--
Posted via http://www.ruby-forum.com/.

Greg Navis

unread,
Jun 3, 2016, 3:30:07 PM6/3/16
to rubyonra...@googlegroups.com
I think it should be enough to add `data = []` after `i = 0` in `process_raw_data`.
--
Greg Navis
I help tech companies to scale Heroku-hosted Rails apps.

nanaya

unread,
Jun 3, 2016, 3:31:11 PM6/3/16
to rubyonra...@googlegroups.com
Hi

On Sat, Jun 4, 2016, at 04:16, Rynn Steinbolt wrote:
> Hello Folks!
>
> Im stuck with my current task, partly because there seems to be a lack
> of basic understanding of syntax i cant pinpoint. I hope you can show me
> a way out!
>
> What Im trying to do is processing one model's data, thus turning it
> into another model's data. It works with one datum, but I cant get it to
> work with the whole data set. I tried several versions of code in
> several places, but this is the current version:
>
> class RawDatum < ActiveRecord::Base
> default_scope :order => 'raw_data.timestamp DESC'
> def self.process_raw_data
> i = 0
> RawDatum.find_each do |raw|
> data[i] = ProcessedDatum.create
> data[i].period_label = raw.status
> i += 1
> end
> return data
> end
> end
>

You need to initialize data variable before doing the loop (`data =
[]`).

def self.process_raw_data
data = []
find_each do |raw|
data << ProcessedDatum.create(period_label: raw.status)
end

data
end

Rynn Steinbolt

unread,
Jun 3, 2016, 3:46:52 PM6/3/16
to rubyonra...@googlegroups.com
Greg Navis wrote in post #1183804:
> <http://www.gregnavis.com/newsletter/>

Thank you, that did the trick! I guess the notion of declaring the type
of an object struck me as un-ruby-ish.

Rynn Steinbolt

unread,
Jun 3, 2016, 3:48:40 PM6/3/16
to rubyonra...@googlegroups.com
nanaya wrote in post #1183805:
Beautiful, thank you! Works like a charm and is indeed much more elegant
than my code.

Greg Navis

unread,
Jun 3, 2016, 3:49:54 PM6/3/16
to rubyonra...@googlegroups.com
I'm glad it helped. It's not about declaring a type of object. `[]` is a syntactic sugar for `Array.new`. It creates a new empty array. So `data = []` (or `data = Array.new`) create a new array and assigns it to `data`.
--
Greg Navis
Reply all
Reply to author
Forward
0 new messages