0

This seems like a pretty trivial problem but i am not able to solve this. I have two tables invoices and invoice_items. In invoice model:

accepts_nested_attributes_for :invoice_items, allow_destroy: true, reject_if: :all_blank

Now in a method based on some conditions i want to add more invoice items dynamically. In before_save callback i am doing something like this:

def process_amounts
  if condition_holds
    self.invoice_items_attributes << {key_1: value_1, key_2: value_2} # Pseudo code.
  end
end

But this piece of code raises errors. Seems like only setter is available for nested_attributes.

NoMethodError Exception: undefined method `invoice_items_attributes' for # Invoice:0x007fd4de84a7a0

I also tried one another approach which results in weird behavior:

def process_amounts
  if condition_holds
    self.invoice_items_attributes = invoice_items.map(&:attributes) + [{key_1: value_1, key_2: value_2}] # Pseudo code.
  end
end

The above piece of code results in 3 items! It doesn't reassign the invoice_items_attributes.

So how to fix this problem?

2 Answers 2

1

Try this

 if condition_holds
    self.invoice_items << InvoiceItem.new(key_1: value_1, key_2: value_2)
 end

I'm assuming InvoiceItem is the model name for invoice_items.

Sign up to request clarification or add additional context in comments.

Comments

0

invoice_item_attributes is only available in params(Controller). You can do this in model invoice in before_save callback like this.

before_save :process_amounts

def process_amounts
 if condition_holds
   self.invoice_items.build([{key: value, key2: value2},{key: value, key2: value2}])
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.