8

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!

4

2 回答 2

11

Basically what you have to do is generate a hash in YAML

default:
  options:
    :something: 2
    :something_else: 3
于 2014-12-02T12:47:45.660 回答
11

我正在使用 Rails 4,这是我的夹具文件,其中 options 是一个 hstore 字段。

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

我不知道如何正确使用哈希,所以我只是对其进行了硬编码。

于 2013-09-12T19:16:02.950 回答