Copy Object Attributes Using Ruby On Rails 3

I copy the attributes of an object from model to another model, for example:

@estimate = Estimate.find(params[:estimate_id]) @invoice = Invoice.create(@estimate.attributes) 

With this copy, only its attributes only copy, (Evaluate copies as a new invoice), but the model of the valuation model and invoice has a HABTM relationship to the "Item" model.

How to create a new @invoice object using Estimate and its Item together? Confused ...

Update:

  • Estate: has_and_belongs_to_many: items (table valu_items) accepts_nested_attributes_for: items
  • Account: has_and_belongs_to_many: items (table invoices_items) accepts_nested_attributes_for: items
  • Element: belongs_to: belongs_to rating: invoice

Thanks.

+4
source share
1 answer

I'm a little confused what exactly you mean here. Do you want to copy both attrs and create an entry in the join table to link the two at the same time? If so, then I have to do the trick (untested) ...

 @estimate = Estimate.find(params[:estimate_id]) @invoice = Invoice.create(@estimate.attributes.merge(:items => @estimate.items)) 
+5
source

Source: https://habr.com/ru/post/1340022/


All Articles