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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/21ff9b4b1039056a2b86cb5a4d2cb332%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Greg Navis at Jun 3, 2016 at 7:29 pm
    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/>

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAA6WWt8i6eRCTy3DAQPMnWNa5RgyKj%2BJ5YQWT5kKvFysNVK_eg%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rynn Steinbolt at Jun 3, 2016 at 7:46 pm

    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.

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6313e41d938536f3b19d862ad2db7e3c%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Greg Navis at Jun 3, 2016 at 7:49 pm
    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
    I help tech companies to scale Heroku-hosted Rails apps.
    Free, biweekly scalability newsletter for SaaS CEOs
    <http://www.gregnavis.com/newsletter/>

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAA6WWt81B6fAp_3ErX7ctB42kKCPMNi3bOHEgz7Mv3L2tCAjvQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nanaya at Jun 3, 2016 at 7:30 pm
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1464982249.844296.627323489.1FE69A85%40webmail.messagingengine.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rynn Steinbolt at Jun 3, 2016 at 7:48 pm

    nanaya wrote in post #1183805:
    Hi
    On Sat, Jun 4, 2016, at 04:16, Rynn Steinbolt wrote:

    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.

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a50e750291d2b9475b67883d1037b0bc%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJun 3, '16 at 7:16p
activeJun 3, '16 at 7:49p
posts6
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase