Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails yaml fixture - specify NULL value

I have a fixture with values like this:

product_four:
     id: 4
     application_id: 1
     title: "oldtitle"
     deleted_at: ~

Setting up a postgresql database for testing. But I can't figure out how to set the deleted_at field to NULL rather than [empty]. I've tried:

deleted_at: :null
deleted_at: <%= nil %>
deleted_at: ~
deleted_at: NULL

And probably a couple more, without luck. Clues for the clueless?

like image 826
Bladt Avatar asked Jan 21 '14 21:01

Bladt


1 Answers

Just leave the value out:

product_four:
     id: 4
     application_id: 1
     title: "oldtitle"
     deleted_at:

For example:

> {:k => ''}.to_yaml
 => "--- \n:k: \"\"\n" 
> {:k => nil}.to_yaml
 => "--- \n:k: \n" 
> YAML.load({:k => nil}.to_yaml)
 => {:k=>nil} 

Note that k: means that k has a nil value whereas k: "" means that k has an empty string as its value.

You could also use an explicit null if all your parsers are aware of the latest YAML spec:

product_four:
     id: 4
     application_id: 1
     title: "oldtitle"
     deleted_at: null

For example:

> YAML.load("--- \n:k: null\n")
 => {:k=>nil} 
like image 163
mu is too short Avatar answered Oct 19 '22 00:10

mu is too short