How to set default value for HStore field in YAML file in Ruby on Rails?

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

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

Being new to HStore, I created a scaffold for Thing ( rails g scaffold Thing field_name:hstore ). The execution of this fixture file ( test/fixtures/things.yml ) did not contain a default value for field_name :

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

This results in a rake test error because there is no value for the required field.

My question is: how to set the value in the YAML file for fixtures for field_name so that my tests pass?

I still know:

  • This one does not work :

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

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

Thanks!

+6
source share
2 answers

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

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

I could not figure out how to use the hash correctly, so I just encoded it.

+9
source

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

 default: options: :something: 2 :something_else: 3 
+8
source

Source: https://habr.com/ru/post/953171/


All Articles