Processing data

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?

I think it should be enough to add `data = []` after `i = 0` in
`process_raw_data`.

Hi

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

Greg Navis wrote in post #1183804:

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.
Free, biweekly scalability newsletter for SaaS CEOs
<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.

nanaya wrote in post #1183805:

Hi

  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

Beautiful, thank you! Works like a charm and is indeed much more elegant
than my code.

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`.