2013-03-21 172 views
0

标题有误导之内从类值,但我不知道字怎么this.I有一个方法获取控制器轨

def showlease 
    @tenant = Tenant.find(params[:id]) 
    @lease = @tenant.lease 


    respond_to do |format| 
    format.html # show.html.erb 
    format.xml { render :xml => @tenant } 
    end 
    end 

所以你可以看到我有表承租人和租赁。有没有一种方法可以从@lease中提取值?我的租赁表有一个名为property_id的值,我希望沿着@ lease.property_id获取某些内容。然后我想用这个值来获取我另一个名为properties的表中的相应记录。这基本上是我想要的伪

@Property = Property.where(_id = (@lease.property_id) 

即使我可以从我的观点做,如果可能的话像

<p> Firstname: <%= @tenant.name %></p> 
<p> LeaseStart: <%= @lease.lease_start %></p> 
    <p> LeaseStart: <%= @property(@lease.property_id).address %></p> 

编辑:

这里是我的模型

class Lease < ActiveRecord::Base 
    attr_accessible :lease_end, :lease_start, :property_id, :tenant_id 
    belongs_to :tenant 
    belongs_to :property 
end 

class Property < ActiveRecord::Base 
    attr_accessible :address_line_1, :city, :county, :image, :rent 
    has_one :lease 
end 

class Tenant < ActiveRecord::Base 
    attr_accessible :email, :name, :phone 
    has_one :lease 
end 
+0

为什么你不能做@ lease.property.address?如果这对您不合适,您可以将地址方法委托给Lease模型。 '委托:地址,以::财产' – Kaeros 2013-03-21 13:36:47

+0

我编辑它以显示我的模型我无法启动它 – user2186771 2013-03-21 13:47:03

回答

0

只要你已经定义了你的模型中的关联,你不需要通过ID找到,你可以ñ只是要求关联的对象。

<%= @lease.property.address %> 
+0

我编辑它以显示我的模型,如果你可以有另一种外观 – user2186771 2013-03-21 13:42:16

+0

你的模型看起来像他们将支持调用如上所述直接关联对象。 – Matt 2013-03-21 13:44:26

+0

我得到这个误差 未定义的方法'属性”为#<租赁:0x007f06a824d640> 提取的源(围绕线#4): 1:

<%= notice %>

2:

姓:<(%)= @ tenant.name%>

3:

开始:<%= @ lease.lease_start%>

4:

属性<%= @ lease.property.address_line_1%> – user2186771 2013-03-21 13:52:51