I have an array that contains hash in it.
[{"id"=>1353899, "client_id"=>727977, "ticket_id"=>1399613, "ticket_ticket_id"=>632355, "goods_quantity"=>143, "firm"=>nil, "cashregister"=>nil, "user_id"=>5048, "created_at"=>"2021-08-25T13:35:15", "started"=>"2021-07-05", "finished"=>"2022-07-04", "returned"=>nil, "account_summ_id"=>6481426, "account_membership_id"=>nil, "recommended"=>nil, "discount"=>"252.0", "discount_kind"=>1, "discount_comment"=>"Сотрудник", "manual_discount"=>true, "fullprice"=>"456.0", "docnumber"=>nil, "membership_summ"=>"0.0", "name"=>"SMART", "ticket_kind_id"=>1, "price"=>"204.0", "duration"=>12, "time_kind_id"=>4, "freeze_count"=>2, "freeze_days"=>61, "passes"=>nil, "tax_percent"=>"20.0", "client_surname"=>"Smetanin", "client_name"=>"Nikita", "client_phone1"=>" 372 5366 9579", "client_phone2"=>nil, "client_email1"=>"[email protected]", "align_to_month"=>0, "auto_continue"=>0, "stop_auto_continue"=>false, "tax_id"=>68, "day_price"=>nil, "together_with_membership"=>0, "promoaction_id"=>nil, "promocode"=>nil, "pay_schedule_option_id"=>nil, "color"=>"#FF9900", "total_limit_passes"=>0, "guest_visits"=>nil, "debt"=>"0.0", "get_next_debt"=>nil, "full_debt"=>"153.0", "debt_membership"=>0, "goods_rest"=>143, "passes_used"=>0, "guest_visits_used"=>0, "freezes_used"=>0, "freeze_days_used"=>0, "author_name"=>"Smetanin Nikita", "users"=>1, "payed"=>true, "schedule_id"=>1708, "status_freeze"=>false, "trainer_id"=>nil, "need_trainer"=>false, "duration_str"=>"12 Month", "passes_str"=>"Not limited", "next_pay"=>"2021-10-05"}]
How do I get the value of "next_pay"=>"2021-10-05"? I've tried this, but it doesn't seem to work
@next_pay_data.each{|client_ticket|
next_pay = Luckyfit.request('GET','/client_tickets/payed_finished/' client_ticket['id'].to_s)
client_ticket['next_pay'] = next_pay[:data]
}
CodePudding user response:
The below code gets all the next_pay
values from the array of hash provided:
hash = [{"id"=>1353899, "client_id"=>727977, "ticket_id"=>1399613, "ticket_ticket_id"=>632355, "goods_quantity"=>143, "firm"=>nil, "cashregister"=>nil, "user_id"=>5048, "created_at"=>"2021-08-25T13:35:15", "started"=>"2021-07-05", "finished"=>"2022-07-04", "returned"=>nil, "account_summ_id"=>6481426, "account_membership_id"=>nil, "recommended"=>nil, "discount"=>"252.0", "discount_kind"=>1, "discount_comment"=>"Сотрудник", "manual_discount"=>true, "fullprice"=>"456.0", "docnumber"=>nil, "membership_summ"=>"0.0", "name"=>"SMART", "ticket_kind_id"=>1, "price"=>"204.0", "duration"=>12, "time_kind_id"=>4, "freeze_count"=>2, "freeze_days"=>61, "passes"=>nil, "tax_percent"=>"20.0", "client_surname"=>"Smetanin", "client_name"=>"Nikita", "client_phone1"=>" 372 5366 9579", "client_phone2"=>nil, "client_email1"=>"[email protected]", "align_to_month"=>0, "auto_continue"=>0, "stop_auto_continue"=>false, "tax_id"=>68, "day_price"=>nil, "together_with_membership"=>0, "promoaction_id"=>nil, "promocode"=>nil, "pay_schedule_option_id"=>nil, "color"=>"#FF9900", "total_limit_passes"=>0, "guest_visits"=>nil, "debt"=>"0.0", "get_next_debt"=>nil, "full_debt"=>"153.0", "debt_membership"=>0, "goods_rest"=>143, "passes_used"=>0, "guest_visits_used"=>0, "freezes_used"=>0, "freeze_days_used"=>0, "author_name"=>"Smetanin Nikita", "users"=>1, "payed"=>true, "schedule_id"=>1708, "status_freeze"=>false, "trainer_id"=>nil, "need_trainer"=>false, "duration_str"=>"12 Month", "passes_str"=>"Not limited", "next_pay"=>"2021-10-05"}]
next_pay_array = hash.map do |value|
value["next_pay"]
end
puts next_pay_array
CodePudding user response:
If the array always has 1 hash in it, you can eassily access the hash based on the index like array[0]
.
So you can change your next_pay
variable to return the hash inside the array like this:
next_pay = Luckyfit.request('GET','/client_tickets/payed_finished/' client_ticket['id'].to_s)[0]
Now the next_pay
variable is the hash you want it to be and from there you can access the keys like this:
client_ticket['next_pay'] = next_pay['next_pay']
And in the end your code would look like this:
@next_pay_data.each{|client_ticket|
next_pay = Luckyfit.request('GET','/client_tickets/payed_finished/' client_ticket['id'].to_s)[0]
client_ticket['next_pay'] = next_pay['next_pay']
}
To make it a bit more readable I would suggest to change some variable names and use string interpolation like this:
@next_pay_data.each{|client_ticket|
next_pay_date = Luckyfit.request('GET',"/client_tickets/payed_finished/#{client_ticket['id']}")[0]['next_pay']
client_ticket['next_pay'] = next_pay_date
}