我有两个类 -pop_vlans
定义logical_interfaces
如下:
逻辑接口.rb
class LogicalInterface < ActiveRecord::Base
has_many :pop_vlans
end
pop_vlans.rb
class PopVlan < ActiveRecord::Base
self.table_name = 'pop_vlans'
belongs_to :logical_interface, :class_name => "LogicalInterface", :foreign_key => "vlan_id"
end
然后在我的控制器中,我试图访问pop_id
相关对象的列,pop_vlans
但出现未定义的错误:
逻辑接口控制器.rb
def update
if params[:id]
@logical_interface = LogicalInterface.find(params[:id])
@pop_id = @logical_interface.pop_vlan.pop_id # error
end
end
但是,我可以获得我想要的属性,但它需要一些额外的行:
@vlan_id = @logical_interface.vlan_id
@pop_vlan = PopVlan.find(@vlan_id)
@pop_id = @pop_vlan.pop_id
但我宁愿让我的脚本更简洁一些(另外,找出上面为什么不起作用,因为它真的让我很烦!)。