[英]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.