ruby on rails - Select attribute which is not current_user.id -
I am creating a messaging system in the railroad, and to display the conversation partners, Current_user is included in:
Message.where ("to_id =? Or user_id =?", Current_user.id, current_user.id)
However, since I want the other user's ID only from that query, I have to select it, and I can either select : to_id
or : user_id Is not
current_user.id
something like this:
Message.where ("to_id =? Or user_id =? ", Current_user.id, current_user.id) .select (: to_id ,: user_id unless current_user.id is not)
Thanks in advance!
Edit: The message table is simplified here:
create_table "messages", force: true do | T | T.integer "to_id" - & gt; The user who has the message t.integer "user_id" - & gt; If you are trying to get a user ID
Message.where ("to_id =? Or user_id =?", Current_user.id, current_user.id). Map {| M M.user_id == current_user.id? M.to_id: m.user_id}
This gives you more sense like
# = & gt; [3771, 3611, 37 9 4, 3638, 36 9 7, 3786]
Comments
Post a Comment