0

I have an array which is result of a query.

r = [
  #<Reservation id: 27, schedule_id: 1, name: "test user", gender: "no_answer",
    reservation_num: 1, zip: "", prefecture: "", address: "", street: "",
    tel: "", note: "", satisfied: true, canceled: false, seq: 27,
    created_at: "2019-08-28 06:04:30", updated_at: "2019-08-28 06:04:30",
    created_by: 2, updated_by: 2, from_partner: false, no_counting: false,
    reservation_time: nil, one_day_payment: nil, payment_id: 123456>
]

I want to get the payment_id but don't understand how should I write.

5 Answers 5

2

If you have number of records in array, you can get payment_id in form of array as below,

r.map(&:payment_id)
Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you should be able to select the first reservation from that array and then call on the payment_id. Like:

r.first.payment_id

Comments

0

To get all payment_id's you can do

r.map(&:payment_id)

Comments

0

You can also use the collect method

r.collect(&:name)

Comments

-1

You can pluck payment ids (from the required set of data) with the following :

Reservation.pluck(:payment_id)

2 Comments

Even though your answer can be right is considered a low quality post because you should write an explanation such is "In ruby the way to retrieve..." which can be applied in the specific case in question or to all cases. Also links to documentation help.
Noted with Thanks 👍.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.