B
B
Bogdan2017-07-27 13:56:39
Ruby on Rails
Bogdan, 2017-07-27 13:56:39

Rails to_json add your key and value?

Hello. Don't tell me, please. How can I add my key and value to the resulting

menu_requirement.to_json(
        only: [ :number ], user: 555
 }

to get the output
{
    "number": "KL-000000053",
    "user': "555"
}

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2017-07-27
@2ord

Correct as_json, not to_json.

h = menu_requirement.as_json(
        only: [ :number ]
)
h[:user] = 555 # Если атрибут :user отсутствует в модели MenuRequirement

# puts h.inspect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question