Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate action fire an insert query?
    primarykey
    data
    text
    <p>Hi i know it's silly but true i got this when i edit any entry.</p> <p>I have an expense model and an expense_line_item and a paid_line_item model, it is created properly when i create a new entry but on editing previous entry it adds a new entry instead of updating, i. e. at update action it fires insert query. here is my code:</p> <p>My controller:</p> <pre><code>def new @menu = 'Expenses' @page_name = 'Record New Expenses' @expense = Expense.new @expense.expense_line_items.build @expense.paid_line_items.build @expense.voucher_number = "EXP"+Time.now.to_i.to_s @from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments') @to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments') respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @expense } end end # GET /expenses/1/edit def edit @menu = 'Expenses' @page_name = 'Edit Expenses Entry' @expense = Expense.find(params[:id]) @from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments') @to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments') end # POST /expenses # POST /expenses.xml def create @expense = Expense.new(params[:expense]) @expense.created_by = current_user.id @expense.company_id = current_company.id respond_to do |format| if @expense.save format.html { redirect_to(@expense, :notice =&gt; 'Expense was successfully created.') } format.xml { render :xml =&gt; @expense, :status =&gt; :created, :location =&gt; @expense } else @menu = 'Expenses' @page_name = 'Record New Expenses' @from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments') @to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments') format.html { render :action =&gt; "new" } format.xml { render :xml =&gt; @expense.errors, :status =&gt; :unprocessable_entity } end end end # PUT /expenses/1 # PUT /expenses/1.xml def update @expense = Expense.find(params[:id]) respond_to do |format| if @expense.update_attributes(params[:expense]) format.html { redirect_to(@expense, :notice =&gt; 'Expense was successfully updated.') } format.xml { head :ok } else @menu = 'Expenses' @page_name = 'Edit Expenses Entry' format.html { render :action =&gt; "edit" } format.xml { render :xml =&gt; @expense.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>My Model:</p> <pre><code> expense model: class Expense &lt; ActiveRecord::Base has_many :expense_line_items has_many :paid_line_items accepts_nested_attributes_for :expense_line_items, :reject_if =&gt; lambda {|a| a[:account_id].blank? } , :allow_destroy =&gt; true accepts_nested_attributes_for :paid_line_items, :reject_if =&gt; lambda {|a| a[:account_id].blank? }, :allow_destroy =&gt; true #validations validates_presence_of :expense_date, :voucher_number validates_presence_of :expense_line_items validates_associated :expense_line_items validates_presence_of :paid_line_items validates_associated :paid_line_items end </code></pre> <p>expense_line_item:</p> <pre><code>class ExpenseLineItem &lt; ActiveRecord::Base belongs_to :expense end </code></pre> <p>paid_line_item:</p> <pre><code>class PaidLineItem &lt; ActiveRecord::Base belongs_to :expense end </code></pre> <p>My form :</p> <pre><code>&lt;%= form_for(@expense) do |f| %&gt; &lt;% @expense.expense_line_items.each_with_index do |expense_line_item, index| %&gt; &lt;%= render "expense_line_items", :expense_line_item =&gt; expense_line_item, :index =&gt; index %&gt; &lt;% end %&gt; &lt;tr id="row_link" valign="top"&gt; &lt;td valign="top" colspan="6"&gt; &lt;%= link_to "Add new row",{:action =&gt; :add_row, :index =&gt; @expense.expense_line_items.size}, :remote =&gt; true %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% @expense.paid_line_items.each_with_index do |paid_line_item, index| %&gt; &lt;%= render "paid_line_items", :paid_line_item =&gt; paid_line_item, :index =&gt; index %&gt; &lt;% end %&gt; &lt;tr id="to_row_link" valign="top"&gt; &lt;td valign="top" colspan="6"&gt; &lt;%= link_to "Add new row",{:action =&gt; :add_to_row, :index =&gt; @expense.paid_line_items.size}, :remote =&gt; true %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p>i got frustrated, thanks in advance . </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload