Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何获取外键的名称_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何获取外键的名称

Ruby on rails 如何获取外键的名称,ruby-on-rails,ruby,Ruby On Rails,Ruby,在我的Rails应用程序中,我有一个设备端口列表视图,每个设备端口都属于一个设备: 设备.rb has_many :device_ports, :foreign_key => "device_id" belongs_to :device def list @device_ports = DevicePort.paginate(:all, :page => params[:page], :per_page => 100, :order => "name")

在我的Rails应用程序中,我有一个设备端口列表视图,每个设备端口都属于一个设备:

设备.rb

has_many :device_ports, :foreign_key => "device_id"
belongs_to :device
def list
    @device_ports = DevicePort.paginate(:all, :page => params[:page], :per_page => 100, :order => "name")
    @devices = Device.find(:all)
end
设备\u端口.rb

has_many :device_ports, :foreign_key => "device_id"
belongs_to :device
def list
    @device_ports = DevicePort.paginate(:all, :page => params[:page], :per_page => 100, :order => "name")
    @devices = Device.find(:all)
end
设备\u端口\u控制器.rb

has_many :device_ports, :foreign_key => "device_id"
belongs_to :device
def list
    @device_ports = DevicePort.paginate(:all, :page => params[:page], :per_page => 100, :order => "name")
    @devices = Device.find(:all)
end
list.rhtml

<table width="100%">
    <tr>
        <th>Name</th>
        <th>Device</th>
    </tr>

    <%= render :partial => 'device_port/list_item', :collection => @device_ports %>
</table>
<tr>
    <td><a href='/device_port/update/<%= list_item.id %>'><%= list_item.name %></a></td>
    <td><%= list_item.device_id %></td>
</tr>

名称
装置
“设备\端口/列表\项”,:collection=>@device\端口%>
_列出项目。rhtml

<table width="100%">
    <tr>
        <th>Name</th>
        <th>Device</th>
    </tr>

    <%= render :partial => 'device_port/list_item', :collection => @device_ports %>
</table>
<tr>
    <td><a href='/device_port/update/<%= list_item.id %>'><%= list_item.name %></a></td>
    <td><%= list_item.device_id %></td>
</tr>

因此,我从
device\u port
表中显示
device\u id
,但实际上我想显示
devices
表的
name

我在我的应用程序的其他地方做了一些非常类似的事情,可能稍微有点困难,我有一个预选的下拉列表,其中选择了正确的设备名称,但我似乎不知道如何在选择菜单的上下文中完成它

我在上面的控制器中的
@devices
变量中有这些设备,并试图使用
列表项查找它们。设备id
,但它似乎不起作用


这可能是一件非常简单的事情,但我似乎找不到获得所需结果的正确方法。

因为设备端口属于设备,所以
列出项目.device.name
谢谢

我刚刚在控制台中测试了这个功能,但它应该可以工作。您可以在返回数组的模型上调用

所以,我刚才使用的是我的用户模型:

User.columns[0].name #=> 'id'
想必是你的

Device.columns[x].name

编辑:我把问题读为“列的名称”

,因为设备端口属于设备so
列表\u项。设备。名称
谢谢!该死的,我知道这很简单,但我对语法一无所知,谢谢你让我保持理智!