Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring and Retrieving OpenStruct from Database?
    text
    copied!<p>I'm currently trying to find the best way to store and retrieve some nested data from a database in Rails 3. I make a call to an API and it returns back and outputs in an OpenStruct format. Some of the data is one level deep and I am parsing this and storing only the value in the database. No problems there. However, some of the items being returned have 100s of sub items and I really don't want to have to make a mess of my tables. So what I've done so far is just save everything second level or deeper directly into the database.</p> <p>Just to give you an example, let's say I'm getting info on a car which I am storing to my cars database. So basically make and model are getting saved out a strings, but let's say a field for specs which has all of the cars specs nested within it in OpenStruct. So rather than going spec1 = car.specs.spec1, spec 2= car.specs.spec2 ... specs100 = car.specs.spec100, etc I'm doing specs = car.specs and saving that to the database. (Very loose example just to give a general idea.)</p> <p>My issue is retrieving that data. Traversing the data doesn't seem to work anymore.</p> <p>If I try and output some of the nested info I get "undefined method".</p> <p>If I output the entire field I get something like: "Car Spec1: --- !ruby/object:OpenStruct table: :spec2: Something Here :spec3: Something Here :spec4: Something Here :spec5: !ruby/object:OpenStruct table: ... etc etc"</p> <p>I am rather new to Rails as well as the OpenStruct format so I'd appreciate any help in trying to retrieve the data. Or perhaps a better way of saving the data in the first place. What am I missing?</p> <p>db/schema.rb:</p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20120401042641) do create_table "characters", :force =&gt; true do |t| t.integer "user_id" t.string "realm" t.string "name" t.integer "level" t.integer "race" t.integer "class_id" t.integer "gender" t.text "guild" t.text "talents" t.text "items" end add_index "characters", ["user_id"], :name =&gt; "index_characters_on_user_id" end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload