2013-03-21 173 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