AnthonyGalli.com AnthonyGalli.com - 1 month ago 15
Ruby Question

Get record from integer?

<%= @duel.duelers.where.not(user_id: current_user.id).first.user_id %>
produces an integer, i.e.
1
, but how can I get it to produce the record from that user_id
#<User:0x007fe476a42948>
?

Answer

You can replace the last user_id with the record you want to show.

e.g.

<%= @duel.duelers.where.not(user_id: current_user.id).first.user_id %>

to (for all records)

<%= @duel.duelers.where.not(user_id: current_user.id).first.user %>

or for a single record:

<%= @duel.duelers.where.not(user_id: current_user.id).first.user_name %>

This part; @duel.duelers.where.not(user_id: current_user.id).first already got you on the user so anything you put behind that will display the record for that instance.

Comments