Search code examples
ruby-on-railspostgresqlyamlruby-on-rails-4hstore

How do I set a default value for an HStore field in a Ruby on Rails fixtures YAML file?


I am trying to use the Ruby on Rails 4.0 HStore extension for PostreSQL. I would like to make one of my HStore fields required:

class Thing < ActiveRecord::Base
  # ...
  validates :field_name, presence: true
  # ...
end

Being new to HStore, I generated a scaffold for Thing (rails g scaffold Thing field_name:hstore). Doing this my fixture file (test/fixtures/things.yml) did not include a default value for field_name:

one:
  # ...
  field_name:
  # ...

Which causes rake test to fail since there is no value provided for a required field.

My question is: How do I set a value in my fixtures YAML file for field_name so that my tests will pass?

So far I know:

  1. This does not work:

    one:
      # ...
      field_name:
        small: 2
        medium: 5
        large: 4
      # ...
    
  2. This also does not work:

    one:
      # ...
      field_name: {"small"=>"2", "medium"=>"5", "large"=>"4"}
      # ...
    

Thanks!


Solution

  • I am using Rails 4 and this is my fixture file, where options is a hstore field.

    default:
      title: 'something'
      prefix: 'xxx'
      options: '"something"=>"2", ""=>"5"'
    

    I couldn't find out how to properly use a Hash so I just hard coded it.