简体   繁体   English

如何从ID获取用户名

[英]How do I get username from the id

I want to get the name of the caller by the caller_id. 我想通过caller_id获取呼叫者的姓名。 Name is in the user table. 名称在用户表中。

Here's the migration file. 这是迁移文件。

class AddCallerIdToSeller < ActiveRecord::Migration
  def change
    add_column :users, :caller_id, :string
  end
end

This is the view that I am currently doing. 这是我目前正在做的观点。

 <td class="text-center"><%= call_report.caller_id.present? %></td>

When i do like above, it shows true. 当我像上面一样时,它显示为true。 But I want to get the user name which is "=" caller_id 但是我想获取的用户名是“ =“ caller_id

User.find(call_report.caller_id).name

在控制器中执行此操作并将其分配给那里的某个变量的最佳方法。

If you do this... 如果你这样做

class CallReport < ApplicationRecord
  belongs_to :user
end

You can then do 然后你可以做

call_report.user.name

Also the ID is normally stored as an integer, so it's more typical to see... ID通常也存储为整数,因此更常见的是...

add_column :users, :caller_id, :integer

I'd do a rake db:rollback then make the above change then rake db:migrate 我会做一个rake db:rollback然后做上面的改变然后rake db:migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 php 中的会话从用户的用户名中获取用户的用户 ID? - How do I get the user ID of a user from their username via sessions in php? 如何合并2个不同表中的select语句,其中我需要表1中的by_user_id用于表2 select语句以获取用户名? - How do I merge select statement from 2 different tables where I need by_user_id from table 1 for table 2 select statment to get username? 如何从表1获取用户名的ID并将其插入表2 - How to get id of username from table 1 and insert that id to table 2 如何将表的 group_id 从主用户的 ID 更改为主用户 SQL 的用户名? - How do I change the group_id of a Table from being an ID of the primary user to being the Username of the primary user SQL? 如果我只知道用户名,如何使用SQL语句从网站获取数据? - How do I use SQL statement to get data from a website if all I know is the username? 如何从3个返回的行集中获取最后一个ID? - How do I get last ID from set of 3 returned rows? 如何从gridview获取图像的ID并增加点击率? - How do i get id of the image from gridview and increase hits? 如何从group by子句中获取表键ID - How do I get the table key id from a group by clause 如何从插入的数据中获取最后一个ID? - How do i get the last ID's from the inserted data? 如何从datagridview中的选定行获取ID? - How do I get the ID from a selected row in a datagridview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM