帮助rails belongs_to

时间:2011-03-15 05:30:39

标签: ruby-on-rails associations belongs-to

我被困 -

我正在使用rails构建会计应用程序,并且有一个模型,其中客户将有多个发票以及多个付款(与这些发票相关)

以下是简要模型的简要介绍:

class Customer < ActiveRecord::Base
  has_many :customer_payments
  has_many :invoices  
  accepts_nested_attributes_for :customer_payments, :allow_destroy => true
end

class CustomerPayment < ActiveRecord::Base
  has_many :customer_payment_items
  belongs_to :customer
  belongs_to :invoice
  accepts_nested_attributes_for :customer_payment_items
end

class CustomerPaymentItem < ActiveRecord::Base
  belongs_to :invoice, :inverse_of => :customer_payment_items
  belongs_to :customer_payment
end

 class Invoice < ActiveRecord::Base
  has_many :invoice_lines, :dependent => :destroy   
  has_many :customer_payment_items, :inverse_of => :invoice
  belongs_to :customer
  accepts_nested_attributes_for :invoice_lines, :allow_destroy => true
end

我有一个嵌套的表单,我想显示Customer属性,CustomerPayment属性和CustomerPaymentItem属性 - 这些都可以正常工作。

我还想为每个CustomerPaymentItem显示Invoice属性(每个CustomerPaymentItem与一张发票有关),虽然我可以获得一个表单来显示CustomerPaymentItem信息,但我无法显示需要的Invoice信息。给用户参考。 - 我无法从belongs_to关联中获取要在表单上显示的数据。

我不知所措 - 我不应该能够遍历belongs_to协会吗?仅供参考 - 我可以将数据发送到我知道在CustomerPayment.new调用期间填充发票数据的日志,它似乎只是在控制器和表单之间丢失。

我应该如何访问这些数据?这是表格信息 - (来自几个渲染形式)未显示的东西介于---之间。

<p>
  <%= f.label :amount %><br />
  <%= f.text_field :amount %>
</p>
<p>
  <%= f.label :invoice_id %><br />
  <%= f.text_field :invoice_id %>
</p>

<p>
  <% f.fields_for :invoice do |builder| %>
    --- doesn't show
    Invoice Detail
    <p>
      <%= builder.label :number %><br />
      <%= builder.text_field :number %>
    </p>  
    <p>   
      <%= builder.label :sub_total %><br />
      <%= builder.text_field :sub_total %>
    </p>
    --- doesn't show

  <% end %>
</p>

我在fields_for中遗漏了哪些内容以显示:发票参考数据?我的模型是否太复杂,以至于轨道无法理解?

1 个答案:

答案 0 :(得分:0)

@corroded是对的 - 问题是我缺少一个=登录

<% f.fields_for :invoice do |builder| %>

我想每个人都需要学习这个课程