尝试在我的 Rails 应用程序中建立关系时,我收到"nil:NilClass1 的未定义方法'主机名''



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

我在class IpsSensorclass IpsEvent中缺少一些东西吗?

Table in The db;

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

My working View;

<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>

My attempt to do a relation;

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

我的传感器表aka 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

这样做,在应用程序帮助器中定义这个方法

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

在你的视图

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

如何让关系在rails上工作?

Rails关联只是用关联表中的数据填充方法的方法。您通常需要使用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 %>

最新更新