标题有误导之内从类值,但我不知道字怎么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
为什么你不能做@ lease.property.address?如果这对您不合适,您可以将地址方法委托给Lease模型。 '委托:地址,以::财产' – Kaeros 2013-03-21 13:36:47
我编辑它以显示我的模型我无法启动它 – user2186771 2013-03-21 13:47:03