3

I am working on a rails application.

I need to save a key pair value like [user_email & user_phone] in a single attribute, I am thinking to use a hash with key user_email & value user_phone.

I have PostgreSQL DB with rails 6.0.3, How can I proceed with creating a hash in my DB ?

1 Answer 1

5

You can try with below steps :-

Add your migration with json type field. PostgreSQL has json and jsonb columns which can natively store your hash/object data and allow you to query against the JSON with ActiveRecord!

class MigrationName def change add_column :table, :column_name, :json, default: {} end end 

Now in json column you can store hash like structure .

Sign up to request clarification or add additional context in comments.