红宝石轨道协会
我正在做一个复杂的协会,我有以下问题红宝石轨道协会
我有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
所以,我怎么能得到客户的帐单地址细节一旦客户生产完毕?
寻求指导。提前致谢。
您正在使用您的视图关系不对应于你已经在你的模型中指定的关系,并使用了错误的ID参数查询数据库。
-
CustomerBill
有:billing_address
,不:billing_addresses
。有一个在show.html.erb
- 没有必要为一个循环您使用
params[:customer_id]
正确查询BillingAddress
。find
为BillingAddress
,打算到: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
方法)。
我喜欢上面的答案。如果你坚持让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%>
等等...... 因此,在这种情况下,'委托'将允许您将对属性和方法的调用委托给另一个类。
谢谢你分享这个答案,我没有想过使用委托选项。并且要清楚我希望客户帐单视图中的帐单地址中的客户详细信息不是客户视图。所以根据你的回答我已经尝试过。我没有得到理想的结果。对此事的看法会很好。我已经在下一条评论中发布了链接:)再次感谢:) – deeraj 2012-08-16 06:08:31
http://pastebin.com/QrL3yxfw只是要清楚我们可以使用委托的一些条件?像'委托:find_by_customer_id'或类似的东西?我浏览了一些关于委托的博客,并没有给出有关委托的条件。 – deeraj 2012-08-16 06:29:43
我想通了。谢谢,并加1回答。 :) – deeraj 2012-08-17 05:26:37
对不起,我没有清楚的问题。帐单创建后,我想要检索客户的帐单地址详细信息。因为'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