Factory Girl with string attribute keys instead of symbols?
Asked Answered
A

2

10

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" }
Arlenarlena answered 12/2, 2012 at 9:12 Comment(0)
L
17

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
Lavalley answered 12/2, 2012 at 9:19 Comment(0)
S
7

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

Factory.attributes_for(:some_class).with_indifferent_access
Samathasamau answered 12/2, 2012 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.