Fabricator turns string keys to symbols

Skip to first unread message


Feb 18, 2019, 1:27:10 PM2/18/19
to fabrication

Sorry if this question has already been asked or I am using not super accurate terminology.

I am fabricating and object like this:

partner = Fabricate(:kinetic_partner, type: type, partner_flags: { "ecommerce": "true" })

But after fabrication the key of partner_flags hash is converted to symbols. 

>> partner.partner_flags

I understand that symbols are a better option here but in our legacy codebase string keys are used and it is hard to change them. Is there a way to keep the string key for ecommerce after fabrication to avoid using with_indifferent_access for making object structure consistent between the actual code and specs?

Thanks for looking at this.


Paul Elliott

Feb 18, 2019, 6:23:39 PM2/18/19
to fabrica...@googlegroups.com
Hey Sepand,

This isn't behavior from Fabrication but from ruby itself. You can verify by opening up irb and entering in your hash. You'll see it immediately converted to have symbol keys.

To get the behavior you're wanting you'll need to call `stringify_keys` on the hash (assuming you have active support). That will look like this.

Fabricate(:kinetic_partner, type: type, partner_flags: { "ecommerce": "true" }.stringify_keys)

Hope that helps!

-- Paul

You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabricationge...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sepand Ansari

Feb 19, 2019, 12:54:03 PM2/19/19
to fabrica...@googlegroups.com
It worked! Thanks for the quick response to my naive question.
Reply all
Reply to author
0 new messages