0

Let's say I have a hash:

{"MemberId"=>"MID-0000001", "MemberName"=>"Bruce", "OrderNumber"=>"ON-000000001"}

How can I insert this hash into orders tables of the db database in mysql?

Actually, this code will be a part of a scheduled task (of the Ruby on Rails app) to be running with whenever gem.

Let me know if I have to submit additional information.

Table orders has the same column names as in the hash.

3
  • Why you don't just use an ActiveRecord model? Commented Jun 7, 2013 at 6:57
  • I've created forms using scaffold and able to create new records. But it doesn't give me an idea how to insert a hash. Can you give some examples? It's my first time to deal with mysql :) Commented Jun 7, 2013 at 7:09
  • @toro2k means here Commented Jun 7, 2013 at 7:30

2 Answers 2

2

Short answer: Active Record, or an alternative "object relational model".

Think of your tables as a schema'd serialization of your hash or object. Ruby, via Active Record, can be pretty intelligent about the de/serialization process, but it typically is defined by the reverse process - by reading your database, Active Record figures out what your data become when de-serialized, and from that, what happens when you serialize it.

So your usual use case would be to figure out what fields you want based on your objects (say, objects.each_with_object({}){|hash, obj| hash.merge!(obj)} and find all your fields), and then write up the Active Record declarations to make that happen. Then Model.create(obj) and you've just saved it to the database

But you're going "backwards", and so may be interested in some interesting features:

Serialization will let you store a pretty arbitrary object into a single field your database (inter-object references may be difficult), effectively mixing shema'd and schema-less paradigms. Postgres has a number of new features some of which are now part of rails 4 via ActiveRecord that will make it easy to do what you want - but both of those options work on a subset of your data. To just dump the whole thing, you can go schemaless and just not worry about it (instead, worry about other things!). Or graph, for completeness, but that doesn't sound like what you're looking for.

TL;DR:

1) Your hashes each describe an object, where each key is a column and each value a row - just define the Active Record model and read in your hash.

2) Stick a subset of the hash into a text column, and parse it out using active record serialization or advanced postgres features.

3) Go with a schema-less database and just stick the whole thing in there as-is.

In the end, your use case is the same: Model.create({"MemberId"=>"MID-0000001", ...})

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time to write a long answer. Actually, I'm already experimenting with mongodb using Mongoid and got stuck with another issue I've posted stackoverflow.com/questions/16952256/… . In the mean time I was feeling curious to know how these would look like with relational database. It was pretty fast to import hashes onto the mongodb because of its schema less feature. Thanks again for your advice.
0
@order = Order.new
@order.member_id = hash['member_id']
|
|
|
|
@order.save

2 Comments

Yes, I wanted such sort of example. In your example you have to specify each hash member, but I'd like to insert at once all the members of the hash. I've tried yours with one hash member like: order = Order.new order.OrderNumber = myXML["OrderNumber"] order.save myXML is hash. This gave me error: uninitialized constant Order (NameError). Here, myXMl is a hash.
What's your model name? change order = MODEL_NAME.new If your model name is Orders you have to do order = Orders.new

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.