What is - !ruby/object:?

I’m trying to get this plugin to work:

It basically gets you all the cities and states in the United States.

However, it reads: “I havent added a rake task to populate the database
yet. But the YAML files are in vendor/plugins/geoinfo/lib/db/*.yml”

Alright, no problem, I’ll do it manually then. However, I look in the
file,
and I find stuff that looks like this:

  • !ruby/object:GeoinfoState
    attributes:
    name: STATE
    abbr: ABBR
    country: COUNTRY
    type: TYPE
    id: “ID”

  • !ruby/object:GeoinfoState
    attributes:
    name: ALASKA
    abbr: AK
    country: UNITED STATES OF AMERICA
    type: STATE
    id: “1”

  • !ruby/object:GeoinfoState
    attributes:
    name: ALABAMA
    abbr: AL
    country: UNITED STATES OF AMERICA
    type: STATE
    id: “2”

  • !ruby/object:GeoinfoState
    attributes:
    name: ARKANSAS
    abbr: AR
    country: UNITED STATES OF AMERICA
    type: STATE
    id: “3”

and so on

The file name is states.yml.

Now, my question is what exactly is this file? How do I use it? The
syntax
doesn’t exactly look like its a fixture…

I’m confused on how I can populate my db tables with that file. Does
anyone
know how? What is that file used for? how?

Thank you.

-David

On Tuesday, April 5, 2011 5:15:32 PM UTC-6, David Z. wrote:

and I find stuff that looks like this:
attributes:
country: UNITED STATES OF AMERICA

and so on

The file name is states.yml.

Now, my question is what exactly is this file? How do I use it? The syntax
doesn’t exactly look like its a fixture…

It is a YAML file (which, by default, your scaffolded fixtures are as
well).
It looks like the YAML was used to serialize some ruby objects.
For example (irb session using 1.8.7):

require ‘yaml’
class GeoinfoState
attr_accessor :attributes
end
x = GeoinfoState.new
x.attributes = { :name => “ALASKA”, :abbr => “AK”, :country => “UNITED
STATES OF AMERICA”, :type => “STATE”, :id => “1” }
puts YAML.dump(x)
— !ruby/object:GeoinfoState
attributes:
:type: STATE
:abbr: AK
:country: UNITED STATES OF AMERICA
:name: ALASKA
:id: “1”

I’m confused on how I can populate my db tables with that file. Does anyone
know how? What is that file used for? how?

One way is to define a simple class (similar to what I did above) or
ensure
the geoinfo gem’s version is loaded. Then do something like:

states = YAML.load_file(…)
states.each do |s|
State.create!(:name => s.name, …) # where State is your model
end

If this isn’t a one-off action, you could even define a method to do
something like this in your State class:

require ‘yaml’
class State < ActiveRecord::Base
def self.import_from(filename)
YAML.load_file(filename).each do |s|
create!(…)
end
end
end

Thank you for the response

So basically it* doesn’t* populate a database table? But instead, I
retrieve
it directly from the yaml file?

-David

On Tuesday, April 5, 2011 5:53:42 PM UTC-6, David Z. wrote:

Thank you for the response

So basically it* doesn’t* populate a database table? But instead, I
retrieve it directly from the yaml file?

-David

Correct. It appears this file was just intended to store serialized
objects,
no database needed.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs