我得到了"未定义的方法`主机名' for nil:NilClass1`试图在我的Rails应用程序中建立关系

时间:2014-05-26 09:36:18

标签: ruby-on-rails activerecord

如何在铁轨上获得关系?当我从数据库中的event表中查看事件时,我希望sid部分显示为主机名,而不是sid。当我将@event.sid更改为@event.ips_sensor.hostname时,我会得到一个未定义的方法hostname' for nil:NilClass1

我错过了class IpsSensorclass IpsEvent中的内容吗?

db中的表;

 sid | cid | signature |         timestamp          
-----+-----+-----------+----------------------------
   1 |   1 |         1 | 2014-05-22 20:50:07.154-04

我的工作视图;

<table>
  <tr>
    <th>Timestamp</th>
    <th>Sensor Name</th>
    <th>Signature</th>
    <th>Signature Class</th>
  </tr>
      <tr>
        <td class='timestamp'><%= @event.timestamp %></td>
        <td class='sensor_name'><%= @event.sid %></td>
        <td class='sig'><%= @event.signature %></td>
        <td class='sig_class'><%= @event.cid %></td>
      </tr>
</table>

我尝试建立关系;

undefined method `hostname' for nil:NilClass
Extracted source (around line #98):

95:       </tr>
96:           <tr>
97:             <td class='timestamp'><%= @event.timestamp %></td>
98:             <td class='sensor_name'><%= @event.ips_sensor.hostname %></td>
99:             <td class='sig'><%= @event.signature %></td>
100:             <td class='sig_class'><%= @event.cid %></td>
101:           </tr>

我的控制器;

class IpsDashboardController < ApplicationController

  def ips_dashboard

    @event = IpsEvent.find(3)

  end
end

我的活动表又名class IpsEvent;

class IpsEvent < ActiveRecord::Base
  attr_accessible :sid, :cid, :signature, :timestamp
  self.table_name = 'event'
  belongs_to :ips_sensor
end

我的传感器表又名class IpsSensor;

class IpsSensor < ActiveRecord::Base
  attr_accessible :sid, :hostname, :interface
  self.table_name = 'sensor'
  has_many :ips_events
end

我想要出现的内容

SELECT hostname from sensor where sid = '1' ;

 hostname     
------------------
 VS-101:dna0:dna1

2 个答案:

答案 0 :(得分:1)

这样做,在你的应用程序助手中定义这个方法

def get_hostname(sid)
  IpsSensor.where("sid =?", sid).first.hostname
end

在你看来

<td class='sensor_name'><%= get_hostname(@event.sid) %></td>

答案 1 :(得分:0)

  

你如何获得在铁轨上工作的关系?

Rails associations只是用关联表中的数据填充方法的方法。您通常需要使用foreign_key来关联数据,但之后,您将能够根据需要访问数据,如下所示:

#app/models/model_1.rb
Class Model1 < ActiveRecord::Base
   has_many :model2
end

#app/models/model_2.rb
Class Model2 < ActiveRecord::Base
   belongs_to :model1
end

#-> @model1.model2

外键

nil:NilClass1

当您收到此错误时,通常表示您的parent变量未填充:

@event.ips_sensor.hostname #-> means @event or ips_sensor won't be populated

我可以看到的问题是,当您尝试访问ips_sensor关联时,您首先使用attr_accessible(您使用的是Rails 3还是4?)

-

我会这样做:

#app/models/ips_event.rb
class IpsEvent < ActiveRecord::Base
   belongs_to :ips_sensor
   #-> schema: id | ips_sensor_id | other | details | created_at | updated_at
   #-> if you want to use other foreign_keys, set with the foreign_key: "column" argument for the association
end

#app/models/ips_sensor.rb
Class IpsSensor < Activerecord::Base
   has_many :ips_events
end

-

#app/controllers/ips_dashboard_controller.rb
def ips_dashboard
    @event = IpsEvent.find 3
end

-

#app/views/ips_dashboards/ips_dashboard.html.erb
<%= @event.ips_sensor.hostname #-> considering ips_sensor has the hostname attribute %>