【问题标题】:Ruby on rails associationRuby on Rails 协会
【发布时间】: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


    【解决方案1】:

    您在视图中使用的关系与您在模型中指定的关系不对应,并使用错误的 id 参数查询数据库。

    1. CustomerBill 有 :billing_address,没有 :billing_addresses。 show.html.erb 中不需要循环
    2. 您使用params[:customer_id] 错误地查询BillingAddress。 find for 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 方法的用途)。

    【讨论】:

    • 对不起,我对这个问题还不清楚。我想在创建账单后检索客户的账单地址详细信息。因为bill 和bill_address 有一个共同的customer_id 字段。我想我可以通过比较 bill 和 bill_address 的 customer_id 获得账单地址、城市等。这就是我想要做的。由于bill_address 和bill 之间没有直接链接,我添加了关联以获取该特定客户的详细信息。我希望我很清楚。
    • 重点是,CustomerBill 有一个 :billing_address 与之关联,所以你可以直接使用它。如果您想获得拥有CustomerBill 的Customer 中的BillingAddress,您可以使用@customer_bill.customer.billing_address。没有必要与我目前看到的所有客户 ID 进行比较。
    • 好吧,我不知道我们可以使用@customer_bill.customer.billing_address。为答案竖起大拇指。 :)
    【解决方案2】:

    我喜欢上面的答案。如果您坚持将 BillingAddress 作为自己的模型,而不是简单地将帐单地址字段添加到 Customer(这将消除混乱和头痛的混乱),那么您可以这样做..(假设上面的控制器代码是 Customer控制器)

    客户模型

    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
    

    客户视图

    <%=@customer.state%>
    <%=@customer.city%>
    <%=@customer.address%>
    <%=@customer.country%>
    <%=@customer.bill_name%>
    <%=@customer.bill_biller%>
    

    等等…… 因此,在这种情况下,“委托”将允许您将对属性和方法的调用委托给另一个类。

    【讨论】:

    • 感谢您分享这个答案,我没想过使用delegate 选项。并且要清楚,我希望客户账单视图中的账单地址中的客户详细信息而不是客户视图。所以根据你的回答,我已经尝试过了。我没有得到想要的结果。对此事轻描淡写会很好。我已经在下一条评论中发布了链接 :) 再次感谢 :)
    • pastebin.com/QrL3yxfw 并且为了清楚我们可以在某些条件下使用委托吗???像delegate :find_by_customer_id 或类似的东西?我浏览了几篇关于委托的博客,但没有关于有条件的委托。
    • 我想通了。谢谢,加1的答案。 :)
    猜你喜欢
    • 2015-04-03
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多