Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory Girl with string attribute keys instead of symbols?

When I call Factory.attributes_for(:some_class) I obviously get back a hash of attributes for that class.

{ :attribute_one => "hello", :attribute_two => "goodbye" }

Is there a convenient way to retrieve this attributes hash with string keys rather than symbols?

{ "attribute_one" => "hello", "attribute_two" => "goodbye" }
like image 607
David Tuite Avatar asked Feb 12 '12 09:02

David Tuite


2 Answers

xdazz's answer is a good option but if you want to actually convert the keys to strings instead of accessing the hash indifferently you can use stringify_keys

Factory.attributes_for(:some_class).stringify_keys
like image 194
aNoble Avatar answered Oct 18 '22 20:10

aNoble


This will let you access the value by both symbol and string key.

Factory.attributes_for(:some_class).with_indifferent_access
like image 7
xdazz Avatar answered Oct 18 '22 20:10

xdazz