我正在做一個複雜的協會,我有以下問題紅寶石軌道協會
我有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
顯示。
我做出賬單地址belongs_to的變化和customer_bill即
我曾嘗試以下。在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
協會 和
<%[email protected]%><br/>
和上面給出控制器中的錯誤和相應的代碼
def show
@customer_bill = CustomerBill.find(params[:id])
@bill = BillingAddress.find(params[:customer_id])
end
所以,我怎麼能得到客戶的帳單地址細節一旦客戶生產完畢?
尋求指導。提前致謝。
對不起,我沒有清楚的問題。帳單創建後,我想要檢索客戶的帳單地址詳細信息。因爲'bill'和'bill_address'有一個共同的'customer_id'字段。我想我可以通過比較'bill'和'bill_address'的customer_id來獲得帳單地址,城市等。這是我想要做的。由於'bill_address'和'bill'之間沒有直接聯繫,我添加了關聯以獲取該特定客戶的詳細信息。我希望我清楚。 – deeraj 2012-08-16 05:56:59
問題是,'CustomerBill' **具有與之相關的**::billing_address',所以您可以直接使用它。如果你想獲得擁有'CustomerBill'的'Customer'的'BillingAddress',你可以做'@ customer_bill.customer.billing_address'。沒有必要對我目前看到的所有內容的客戶ID進行比較。 – deefour 2012-08-16 17:35:04
好吧,我不知道我們可以使用@ customer_bill.customer.billing_address。大拇指的答案。 :) – deeraj 2012-08-17 03:27:20