i am doing a complex association and i have the following problem
i have 2 models. Custmer_bills and Billing_address and both have customer_ids in it. one customer has one billing address and one customer has many customer bills. assoication is as given below.
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
now with this association i am attempting billing address details like address, city, phone-no and displaying in show.html
after the bill is created.
i made a belongs_to change in billing address and customer_bill i.e
i have tried the following. in 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 %>
there is no error in the above code but it does not display the details .
for this i had
has_and_belongs_to_many
association
and
<%=@bill.address%><br/>
and the above gives an error and corresponding code in controller
def show
@customer_bill = CustomerBill.find(params[:id])
@bill = BillingAddress.find(params[:customer_id])
end
so how can i get the billing address details of a customer once the customerbill is made??
Seeking guidance. Thanks in advance.
You are using relations in your view that don't correspond to the relations you've specified in your models, and querying the database using the wrong id parameters.
CustomerBill
has :billing_address
, not :billing_addresses
. There is no need for a loop in show.html.erb
BillingAddress
using params[:customer_id]
. find
for BillingAddress
is going to search by the :id
column, not the customer_id
column.Based on the information you've provided so far, @bill
below is wrong and unnecessary.
def show
@customer_bill = CustomerBill.find(params[:id])
@bill = BillingAddress.find(params[:customer_id])
end
If params[:id]
is the id value of a CustomerBill
, you can get the BillingAddress
from the CustomerBill
instance without directly querying `BillingAddress.
def show
@customer_bill = CustomerBill.find(params[:id])
end
# in your view
<%= @customer_bill.billing_address.address %>
Also, since CustomerBill
only has one BillingAddress
, there's no need for the loop
<% 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 %>
Can simply be
<strong>Billing Address</strong>
<%=h @customer_bill.billing_address.address%><br/>
<strong>City</strong>
<%=h @customer_bill.billing_address.city%><br/>
Again, this is all based on the information you've provided. The way you're trying to use relations in your view does not reflect how your relations are setup in the models, so I'm making some assumptions about what you're trying to do (since, for example, you never specified what this was a show
method for).