i have to following method
before_save :save_each_item_details
def save_each_item_details
items = itemname.length
i = 0
while i < items
items = itemname.length
if !ItemsCensu.exists?(itname: itemname[i], year: "#{date.to_s.split('-').first}")
ItemsCensu.create(itname: itemname[i], monadaM: mm[i], quntity: quantity[i], price: price[i], tax: tax[i], year: "#{date.to_s.split('-').first}", num_invoice << invoice_num)
i += 1
else
puts "test"
i += 1
end
end
end
num_invoice is the array from database. invoice_num is a number like 1239
Each time I save it, I want it to to add the invoice_num into num_invoice[] without removing the old value.
For example
1st save:
invoice_num = 1234
num_invoice << invoice_num
# => 1234
2nd save:
invoice_num = 12345
num_invoice << invoice_num
# => [1234, 12345]
Is there a way to build this into my ItemsCensu.create, something like ItemsCensu.create(num_invoice: << invoice_num)?
invoice_numcoming from? Isnum_invoicedatabase field of current object that you're saving?createexpects a hash of name-value. E.g.Exapmple.create(num_invoice: num_invoice << invouce_num)I'm slightly confused about your data structure, but maybe you wantItemsCensu.create(itname: itemname[i], <whatever else you have here except last>, num_invoice: [invoice_num]. Since you create a new object the value of the array will be your new element inside new array.items_censu.update_attributes(num_invoice: items_censu.num_invoice + invoice num)solve your problem? Simply append the value to it's current array before saving