Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, so I think I've got something that works. I'm not convinced at all that it's elegant, but it gives me the result. I'll spend some time later trying to make it cleaner.</p> <p>The code looks like this:</p> <pre><code>file.each_line do |line| objectType = line[/^{"(\w*)":(.*)}/, 1] objectJSON = line[/{"(\w*)":(.*)}/, 2] objectHash = JSON.parse(objectJSON) case objectType when 'foo' restoredFoo = Foo.new(objectHash.except('id', 'created_at', 'updated_at')) restoredFoo.created_at = objectHash['created_at'] restoredFoo.updated_at = objectHash['updated_at'] restoredFoo.save! end when 'bar' restoredBar = Bar.new(objectHash.except('id', 'created_at', 'updated_at')) restoredBar.created_at = objectHash['created_at'] restoredBar.updated_at = objectHash['updated_at'] restoredBar.save! end end </code></pre> <p>Items of note:</p> <ol> <li>I feel like there should be a way to create the object that isn't a JSON.parse, but rather would make use of the from_json method on the model. I'm not sure what the from_json is good for if it doesn't do this!!</li> <li>I'm having fun with mass_assignment. I don't really want to use :without_protection => true, although this would be an option. My concern is that I do want the created_at and updated_at to be restored as they were, but I want a new id. I'm going to be doing this for a number of entities in my application, I didn't really want to end up replicating the attributes_protected in the code - it seems not very DRY</li> <li>I'm still pretty sure my reg exp can give me both objectType and objectJSON in one call</li> </ol> <p>But having said all that, it works, which is a good step forwards.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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