Ruby on rails 在RubyonRails中获取关系n:m的对象
我有两种型号:Ruby on rails 在RubyonRails中获取关系n:m的对象,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有两种型号: 性能 模数 它们之间的关系是:多对多,有一个表映射关系:modulos_perfiles 我需要得到属于perfil的所有“模”。。我有这个: <% @perfiles.each do |perfil| %> <% @m = perfil.modulo.last %> <%= @m.ruta %><br/> <% end %> 但我得到了这个错误: nil的未定义方法“ruta”:NilClass
<% @perfiles.each do |perfil| %>
<% @m = perfil.modulo.last %>
<%= @m.ruta %><br/>
<% end %>
但我得到了这个错误:
nil的未定义方法“ruta”:NilClass
其中“ruta”是“模量”表的列
我做了这个:
<% @perfiles.each do |perfil| %>
<% @m = perfil.modulo.last %>
<%= debug @m %><br/>
<% end %>
<% @perfiles.each do |perfil| %>
<% perfil.modulo.each do |modulo| %>
<%= modulo.ruta %><br/>
<% end %>
<% end %>
我可以看到@m object的所有属性,所以:
ruby/object:模
属性:
- 身份证号码:7
- 描述:布斯克达斯
- 鲁塔:/busquedas
- 创建时间: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
class Perfil
**
答复
**
我没有足够的声誉来公布答案
我决定:
我做了这个:
<% @perfiles.each do |perfil| %>
<% @m = perfil.modulo.last %>
<%= debug @m %><br/>
<% end %>
<% @perfiles.each do |perfil| %>
<% perfil.modulo.each do |modulo| %>
<%= modulo.ruta %><br/>
<% end %>
<% end %>
所以我可以得到对象“模”的任何属性
谢谢。
<% @perfiles.each do |perfil| %>
<% perfil.modulo.each do |modulo| %>
<%= modulo.ruta %><br/>
<% end %>
<% end %>
共享这两个模型类可能会有所帮助