如何通过关联更改现有模型属性

时间:2015-04-15 14:42:01

标签: ruby-on-rails ruby associations foreign-key-relationship

我有两张桌子:发票和Invoice_Line_Items,发票has_many Invoice_Line_items。 Invoice_Line_Items belongs_to Invoice。 Invoice在Invoice_line_items中有其外键。当我创建新发票时,我选择不同的Invoice_line_items然后保存。但是,当我去检查Invoice_line_item时,其invoice_id:为零。它不会更新每个Invoice_line_item中的invoice_id外键。

更新

我确实在父(发票)之前保存子项目。

模型

class Invoice < ActiveRecord::Base
  belongs_to :client
  has_many :proposal_line_items, dependent: :destroy
  has_many :suppliers, :through => :proposal_line_items
end


class Proposal_Line_Item < ActiveRecord::Base
  belongs_to :proposal
  belongs_to :supplier
  belongs_to :invoice
end

form_for发票

<%= form_for(@invoice) do |f| %>
  <% if @invoice.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2>
      <ul>
      <% @invoice.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
   <div class="field">
    <%= f.label :fecha_de_expedicion %><br>
    <%= f.datetime_select :fecha_de_expedicion %>
  </div>
    <div class="field">
    <%= f.label :invoice_line_item %><br>
    <%= collection_select( :invoice_line_item, :invoice_line_item_id, @invoice_line_items, :id, :supplier_id,  {}, {:multiple => true}) %>
    </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

谢谢

0 个答案:

没有答案