5 Replies - 1183 Views - Last Post: 17 February 2014 - 09:01 PM Rate Topic: -----

#1 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Compiling Array Dynamically Rails/Ruby

Posted 16 February 2014 - 01:20 PM

Hey guys, I'm having an issue compiling an array of products using semantics. I can get all of the data required etc, and everything is working fine, the problem comes when I try to build an array within an array dynamically; the first column being the product platform and the second being the actual product details.

This is what I have so far:

@records.each do |record|
  record.each do |product|
    if product == "results"
      derp ||= 1
    elsif derp == 1
      product.each do |details|
        @final_products = {details["platform"] => details}
      end
    end
  end
end



So basically it all works, but I'm not sure how to compile a hash/array var to store a column with the platforms and then in the next column will be the actual contents of the details var. I basically want to pull all the platforms from the products and create a hash based on the platform name and then store all the details within that hash in a hash or something similar.

It's a rather confusing explanation, so if you need clarification don't hesitate to ask :)/>

This post has been edited by Lemur: 16 February 2014 - 01:32 PM
Reason for edit:: fixed odd indentation


Is This A Good Question/Topic? 0
  • +

Replies To: Compiling Array Dynamically Rails/Ruby

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1381
  • View blog
  • Posts: 3,511
  • Joined: 28-November 09

Re: Compiling Array Dynamically Rails/Ruby

Posted 16 February 2014 - 01:36 PM

Going for the clarification here, that seems like a pretty odd way to make a data structure. That seems to be more of your problem.

Your final products is overwriting each time it hits, which may not be intended.

EDIT - If possible, use pp to get a result object, and post it:

pp @records.first



It'll log to the console, copy and paste it.

This post has been edited by Lemur: 16 February 2014 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Re: Compiling Array Dynamically Rails/Ruby

Posted 16 February 2014 - 03:19 PM

I don't see how it's a silly structure? I'm looking to create a structure to store the data then pass to the view and print based on categories. It's much better than looping through all the categories and checking each and every record.

If you can suggest a better alternative, then by all means enlighten me, I'm all ears :)

This post has been edited by mccabec123: 16 February 2014 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1381
  • View blog
  • Posts: 3,511
  • Joined: 28-November 09

Re: Compiling Array Dynamically Rails/Ruby

Posted 16 February 2014 - 08:41 PM

Can you post a sample of the data, as mentioned above?

If you're filtering things, this is probably a case for map:
Person = Struct.new(:name, :age)

people = [
  Person.new('foo', 42), Person.new('bar', 42)
]

people.each.map(&:age)
# => [42, 42]


This post has been edited by Lemur: 16 February 2014 - 08:42 PM

Was This Post Helpful? 1
  • +
  • -

#5 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Re: Compiling Array Dynamically Rails/Ruby

Posted 17 February 2014 - 03:41 AM

Oh sorry, didn't realise you requested that. Here's a sample output for a game 'Tekken'. This is one of the products, but there is a lot more than one
obviously.

{ "brand" : "ShootFighter Tekken",
  "cat_id" : "11124",
  "category" : "Action",
  "created_at" : 1357812719,
  "genre" : "fighting-action-game-genre",
  "geo" : [ "usa" ],
  "images_total" : 0,
  "manufacturer" : "CENTRAL PARK MEDIA",
  "name" : "ShootFighter Tekken - Complete Collection",
  "offers_total" : 5,
  "platform" : "playstation 3",
  "price" : "19.98",
  "price_currency" : "USD",
  "sem3_help" : "To view image links and additional merchants for this product, please upgrade your plan.",
  "sem3_id" : "1r7BOnBDee882mq2UAyCaK",
  "updated_at" : 1392035685,
  "weight" : "453592.37"
}



I need to use the platform element to build the categories and then store all this data within the categories in a product list within the categories for display in the view.

I was thinking about map myself, but I wasn't quite sure how to apply it, since I haven't had much experience with it. And as for struct I didn't even know you could do this in Ruby, haha, thanks for showing me this. I've used structs in C, but never in Ruby.

Thanks.

This post has been edited by mccabec123: 17 February 2014 - 03:46 AM

Was This Post Helpful? 0
  • +
  • -

#6 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1381
  • View blog
  • Posts: 3,511
  • Joined: 28-November 09

Re: Compiling Array Dynamically Rails/Ruby

Posted 17 February 2014 - 09:01 PM

So basically you want to filter by platform and category here? You CAN do that in Ruby, but you should be doing that with your query. No sense in going through more data than you have to.

Using some faked data, here's what I can get on the Ruby end:
Game = Struct.new(:name, :platform, :category)

# Not suggested, just making some quick hack data here.
25.times.reduce([]) { |a, _| a << Game.new(('a'..'z').to_a.sample(rand(10).to_i + 1).join, %w(ps1 ps2 ps3).sample(1)[0], %w(action adventure rpg shooter).sample(1)[0]) }

# Creates
#=> [
 #<struct Game name="umt", platform="ps1", category="rpg">,
 #<struct Game name="mxpy", platform="ps3", category="shooter">,
 #<struct Game name="qgvfi", platform="ps1", category="adventure">,
 #<struct Game name="bmgovkzy", platform="ps2", category="action">,
 #<struct Game name="q", platform="ps3", category="rpg">,
 #<struct Game name="oyeicpb", platform="ps1", category="action">,
 #<struct Game name="l", platform="ps2", category="shooter">,
 #<struct Game name="mbriqchz", platform="ps3", category="shooter">,
 #<struct Game name="cgidentvba", platform="ps3", category="shooter">,
 #<struct Game name="eowlxcd", platform="ps1", category="shooter">,
 #<struct Game name="bcgutpyxv", platform="ps2", category="adventure">,
 #<struct Game name="kl", platform="ps3", category="action">,
 #<struct Game name="fka", platform="ps1", category="action">,
 #<struct Game name="pztc", platform="ps2", category="shooter">,
 #<struct Game name="uvjgotmyz", platform="ps3", category="rpg">,
 #<struct Game name="nzjkl", platform="ps3", category="shooter">,
 #<struct Game name="rlnkecaogj", platform="ps2", category="shooter">,
 #<struct Game name="lgkidm", platform="ps3", category="rpg">,
 #<struct Game name="mwxltkedso", platform="ps1", category="adventure">,
 #<struct Game name="bjdlkowep", platform="ps2", category="rpg">,
 #<struct Game name="wtzhd", platform="ps1", category="shooter">,
 #<struct Game name="xuh", platform="ps3", category="shooter">,
 #<struct Game name="fpzrsumt", platform="ps2", category="adventure">,
 #<struct Game name="knx", platform="ps1", category="rpg">,
 #<struct Game name="yozvrnbcjw", platform="ps2", category="shooter">]

games.group_by(&:platform).map { |console, games| [console, games.group_by(&:category)] }.to_h

# Returns
{
  "ps1" => {
    "rpg" => [
      #<struct Game name="umt", platform="ps1", category="rpg">,
      #<struct Game name="knx", platform="ps1", category="rpg">
    ],
    "adventure" => [
      #<struct Game name="qgvfi", platform="ps1", category="adventure">,
      #<struct Game name="mwxltkedso", platform="ps1", category="adventure">
    ],
    "action" => [
      #<struct Game name="oyeicpb", platform="ps1", category="action">,
      #<struct Game name="fka", platform="ps1", category="action">
    ],
    "shooter" => [
      #<struct Game name="eowlxcd", platform="ps1", category="shooter">,
      #<struct Game name="wtzhd", platform="ps1", category="shooter">
    ]
  },
  "ps3" => {
    "shooter" => [
      #<struct Game name="mxpy", platform="ps3", category="shooter">,
      #<struct Game name="mbriqchz", platform="ps3", category="shooter">,
      #<struct Game name="cgidentvba", platform="ps3", category="shooter">,
      #<struct Game name="nzjkl", platform="ps3", category="shooter">,
      #<struct Game name="xuh", platform="ps3", category="shooter">
    ],
    "rpg" => [
      #<struct Game name="q", platform="ps3", category="rpg">,
      #<struct Game name="uvjgotmyz", platform="ps3", category="rpg">,
      #<struct Game name="lgkidm", platform="ps3", category="rpg">],
    "action" => [
      #<struct Game name="kl", platform="ps3", category="action">
    ]
  },
  "ps2" => {
    "action" => [
      #<struct Game name="bmgovkzy", platform="ps2", category="action">
    ],
    "shooter" => [
      #<struct Game name="l", platform="ps2", category="shooter">,
      #<struct Game name="pztc", platform="ps2", category="shooter">,
      #<struct Game name="rlnkecaogj", platform="ps2", category="shooter">,
      #<struct Game name="yozvrnbcjw", platform="ps2", category="shooter">
    ],
    "adventure" => [
      #<struct Game name="bcgutpyxv", platform="ps2", category="adventure">,
      #<struct Game name="fpzrsumt", platform="ps2", category="adventure">
    ],
    "rpg" => [
      #<struct Game name="bjdlkowep", platform="ps2", category="rpg">
    ]
  }
}




I'm only really using structs for mock data, but it shows what it needs to regardless. This should be done on the SQL side if at all possible. Ideally you want to not load all of this at once and only load things as they become necessary, otherwise your performance is toast.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1