1

我正在做一個複雜的協會,我有以下問題紅寶石軌道協會

我有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 

所以,我怎麼能得到客戶的帳單地址細節一旦客戶生產完畢?

尋求指導。提前致謝。

回答

1

您正在使用您的視圖關係不對應於你已經在你的模型中指定的關係,並使用了錯誤的ID參數查詢數據庫。

  1. CustomerBill:billing_address:billing_addresses。有一個在show.html.erb
  2. 沒有必要爲一個循環您使用params[:customer_id]正確查詢BillingAddressfindBillingAddress,打算到:id欄搜索,customer_id列。

根據你目前提供的信息,@bill以下是錯誤和不必要的。

def show 
    @customer_bill = CustomerBill.find(params[:id]) 
    @bill = BillingAddress.find(params[:customer_id]) 
end 

如果params[:id]是價值CustomerBill的id,你可以從CustomerBill實例BillingAddress沒有直接查詢`BillingAddress。

def show 
    @customer_bill = CustomerBill.find(params[:id]) 
end 

# in your view 
<%= @customer_bill.billing_address.address %> 

而且,由於CustomerBill只有一個BillingAddress,沒有必要爲循環

<% 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 %> 

可以簡單地

<strong>Billing Address</strong> 
    <%=h @customer_bill.billing_address.address%><br/> 

    <strong>City</strong> 
    <%=h @customer_bill.billing_address.city%><br/> 

再次,這是所有基於信息你提供了。你試圖在你的視圖中使用關係的方式並不能反映你的關係是如何在模型中設置的,所以我對你想要做什麼做了一些假設(例如,因爲你從未指定過什麼這是一個show方法)

+0

對不起,我沒有清楚的問題。帳單創建後,我想要檢索客戶的帳單地址詳細信息。因爲'bill'和'bill_address'有一個共同的'customer_id'字段。我想我可以通過比較'bill'和'bill_address'的customer_id來獲得帳單地址,城市等。這是我想要做的。由於'bill_address'和'bill'之間沒有直接聯繫,我添加了關聯以獲取該特定客戶的詳細信息。我希望我清楚。 – deeraj 2012-08-16 05:56:59

+0

問題是,'CustomerBill' **具有與之相關的**::billing_address',所以您可以直接使用它。如果你想獲得擁有'CustomerBill'的'Customer'的'BillingAddress',你可以做'@ customer_bill.customer.billing_address'。沒有必要對我目前看到的所有內容的客戶ID進行比較。 – deefour 2012-08-16 17:35:04

+0

好吧,我不知道我們可以使用@ customer_bill.customer.billing_address。大拇指的答案。 :) – deeraj 2012-08-17 03:27:20

1

我喜歡上面的答案。如果你堅持讓BillingAddress作爲自己的模型,而不是簡單地向客戶添加帳單地址字段(這將消除混亂和頭痛),那麼你可以這樣做(這是假設上面的控制器代碼是客戶控制器)

客戶型號

class Customer < ActiveRecord::Base 
has_one :billing_address 
has_many :customer_bills 
delegate :state, :city, :address, :country, :to => :billing_address 
delegate :bill_name, :bill_biller, :more_bill_attributes, :to => :customer_bill 
end 

客戶控制器

def show 
@customer = Customer.find(params[:id]) 
end 

客戶視圖

<%[email protected]%> 
<%[email protected]%> 
<%[email protected]%> 
<%[email protected]%> 
<%[email protected]_name%> 
<%[email protected]_biller%> 

等等...... 因此,在這種情況下,'委託'將允許您將對屬性和方法的調用委託給另一個類。

+0

謝謝你分享這個答案,我沒有想過使用委託選項。並且要清楚我希望客戶帳單視圖中的帳單地址中的客戶詳細信息不是客戶視圖。所以根據你的回答我已經嘗試過。我沒有得到理想的結果。對此事的看法會很好。我已經在下一條評論中發佈了鏈接:)再次感謝:) – deeraj 2012-08-16 06:08:31

+0

http://pastebin.com/QrL3yxfw只是要清楚我們可以使用委託的一些條件?像'委託:find_by_customer_id'或類似的東西?我瀏覽了一些關於委託的博客,並沒有給出有關委託的條件。 – deeraj 2012-08-16 06:29:43

+0

我想通了。謝謝,並加1回答。 :) – deeraj 2012-08-17 05:26:37