0

发票有许多发票条目:

class Invoice < ActiveRecord::Base
  has_many :invoice_entries, :autosave => true, :dependent => :destroy
  validates_presence_of :date
end

class InvoiceEntry < ActiveRecord::Base
  belongs_to :invoice
  validates_presence_of :description
end

假设我们在数据库中有一张发票:

id: 1
date: '2013-06-16'

它有两个发票条目:

id: 10                           id: 11
invoice_id: 1                    invoice_id: 1
description: 'do A'              description: 'do C'

现在,我有了新的发票条目:

id: 10                               
description: 'do B'              description: 'do D'

(Existing invoice entry          (New invoice entry
 with updated description)        without id)

我希望发票只有这些新的发票条目(这意味着id=11应该删除发票条目)。

invoice.invoice_entries = new_invoice_entries似乎做了一半的工作。它使用 删除发票条目id=11,使用 description 创建新发票条目'Do D'但不会使用id=10from 'Do A'to'Do B'更新发票条目的描述。我猜当 Rails 看到一个存在id的 in 时new_invoice_entries,它会完全忽略它。真的吗?如果是,这背后的理由是什么?

我的完整代码如下。你会如何解决这个问题?(我使用 Rails 4,以防它简化代码。)


# PATCH/PUT /api/invoices/5
def update
  @invoice = Invoice.find(params[:id])
  errors = []

  # Invoice entries
  invoice_entries_params = params[:invoice_entries] || []
  invoice_entries = []

  for invoice_entry_params in invoice_entries_params
    if invoice_entry_params[:id].nil?
      invoice_entry = InvoiceEntry.new(invoice_entry_params)
      errors << invoice_entry.errors.messages.values if not invoice_entry.valid?
    else
      invoice_entry = InvoiceEntry.find_by_id(invoice_entry_params[:id])

      if invoice_entry.nil?
        errors << "Couldn't find invoice entry with id = #{invoice_entry_params[:id]}"
      else
        invoice_entry.assign_attributes(invoice_entry_params)
        errors << invoice_entry.errors.messages.values if not invoice_entry.valid?
      end
    end

    invoice_entries << invoice_entry
  end

  # Invoice
  @invoice.assign_attributes(date: params[:date])

  errors << @invoice.errors.messages.values if not @invoice.valid?

  if errors.empty?
    # Save everything
    @invoice.invoice_entries = invoice_entries
    @invoice.save

    head :no_content
  else
    render json: errors.flatten, status: :unprocessable_entity
  end
end
4

2 回答 2

0

要不仅更改关联,还更改关联对象的属性,您必须使用accepts_nested_attributes_for

class Invoice < ActiveRecord::Base
  has_many :invoice_entries, :autosave => true, :dependent => :destroy
  validates_presence_of :date
  accepts_nested_attributes_for :invoice_entries, allow_destroy: true
end

Railscast 第 196 集介绍了如何使用nested_attributes.

附录:

accepts_nested_attributes_for期望嵌套哈希中嵌套模型的属性,即:

invoice_params={"date" => '2013-06-16', 
  "invoice_entries_attributes" => [
    {"description" => "do A"},
    {"description" => "do B"}]
}

invoice= Invoice.new(invoice_params)
invoice.save

保存saveinvoice两个invoice_items

现在

invoice=Invoice.find(1)
invoice_params={
  "invoice_entries_attributes" => [
    {"description" => "do A"},
    {"description" => "do C"}]
}
invoice.update_attributes(invoice_params)

删除项目do B并添加项目do C

form_fields可用于创建产生精确嵌套哈希的表单。
有关详细信息,请参阅 railscast。

于 2013-06-16T14:32:20.013 回答
0

尝试使用accepts_nested_attributes_for. 这会清理你的很多代码!这是一个例子:

class Invoice < ActiveRecord::Base
  has_many :invoice_entries, :dependent => :destroy
  validates_presence_of :date
  attr_accessible :invoice_entries_attributes

  accepts_nested_attributes_for :invoice_entries, :allow_destroy => true
end

In the view can you then use fields_for (simple_fields_for with simple form, and semantic_fields_for with formtastic if you use one of these gems).

<%= form_for @invoice do |invoice_form| %>
  <%= invoice_form.fields_for :invoice_entries do |invoice_entry_form| %>
    <%= invoice_entry_form.text_field :description %>
    <%= invoice_entry_form.check_box :_destroy %>
  <% end %>
<% end %>

In you controller can you now refactor down to the basics:

# PATCH/PUT /api/invoices/5
def update
  @invoice = Invoice.find(params[:id])
  if @invoice.update_attributes(params[:invoice]) # This also saves all associated invoice entries, and destroy all that is marked for destruction.
    head :no_content
  else
    render json: @invoice.errors.flatten, status: :unprocessable_entity
  end
end

You can read more about accepts_nested_attributes_for here: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Or you can watch this railscast about nested models: http://railscasts.com/episodes/196-nested-model-form-revised

于 2013-06-16T14:34:39.667 回答