0

我有两个模型:

  1. 文件
  2. 模数

并且它们之间的关系是:Many to Many,有一个表来映射关系:modulos_perfiles

我需要得到属于“perfil”的所有“模数”。我有这个:

<% @perfiles.each do |perfil| %>
    <% @m = perfil.modulo.last %>
    <%= @m.ruta %><br/>
<% end %>

但我收到此错误:

nil:NilClass 的未定义方法“ruta”

其中“ruta”是“模”表的一列。

我做的:

<% @perfiles.each do |perfil| %>
    <% @m = perfil.modulo.last %>
    <%= debug @m %><br/>
<% end %>

我可以看到 @m 对象的所有属性,所以:

红宝石/对象:模

属性:

  • 编号:7
  • 描述:布斯克达斯
  • 芸香:/busquedas
  • created_at: 2012-11-25 02:23:51.984916000 Z
  • 更新时间:2012-11-25 02:23:51.984916000 Z

但我不明白为什么我无法通过以下方式获得此属性:

<%= @m.ruta %>

有什么想法吗,谢谢!

更新

我的模型类是:

class Perfil < ActiveRecord::Base
    has_many :usuario
    has_and_belongs_to_many :modulo
end

class Modulo < ActiveRecord::Base
    has_and_belongs_to_many :perfiles
end

class ModulosPerfiles < ActiveRecord::Base
end

**

回答

**

我没有足够的声誉来发布答案。

我已经解决了:

我做的:

<% @perfiles.each do |perfil| %>
    <% perfil.modulo.each do |modulo| %>
        <%= modulo.ruta %><br/>
    <% end %>
<% end %>

所以我可以得到对象“模”的任何属性。

谢谢。

4

1 回答 1

0
<% @perfiles.each do |perfil| %>
    <% perfil.modulo.each do |modulo| %>
        <%= modulo.ruta %><br/>
    <% end %>
<% end %>
于 2012-12-07T20:49:07.063 回答