【发布时间】:2012-08-14 10:55:54
【问题描述】:
我正在做一个复杂的关联,我有以下问题
我有 2 个模型。 Custmer_bills 和 Billing_address 都包含 customer_ids。一位客户有一个帐单地址,一位客户有许多客户账单。关联如下。
class Customer < ActiveRecord::Base
has_one :billing_address
has_many :customer_bills
end
class CustomerBill < ActiveRecord::Base
attr_accessible :customer_id
belongs_to :billing_address
belongs_to :customer
end
class BillingAddress< ActiveRecord::Base
attr_accessible :customer_id, :address, :city, :phone_work, :phone_home
belongs_to :customer
has_many :customer_bills
end
现在有了这个关联,我正在尝试账单地址详细信息,例如地址、城市、电话号码,并在账单创建后显示在 show.html。
我在帐单地址和 customer_bill 中进行了 belongs_to 更改,即
我尝试了以下方法。在show.html
<% for billing_address in @customer_bill.billing_addresses %>
<strong>Billing Address</strong>
<%=h billing_address.address%><br/>
<strong>City</strong>
<%=h billing_address.city%><br/>
<% end %>
上面的代码没有错误但是没有显示细节。
为此我有
has_and_belongs_to_many协会
和
<%=@bill.address%><br/>
上面给出了控制器中的错误和相应的代码
def show
@customer_bill = CustomerBill.find(params[:id])
@bill = BillingAddress.find(params[:customer_id])
end
那么,一旦制作了客户账单,我如何才能获得客户的账单地址详细信息??
寻求指导。提前致谢。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord associations model-associations