vendredi 21 avril 2017

Ruby on Rails: how to copy objects from one category to another

I'm trying to copy some latest recipients (subscribers) to a new category in my newsletter app. I ran following lines of code in Rails console but instead of copying recipients to new category it moved latest 10 recipients to new category. It looks like recipient.clone is not working. I'm using Ruby v2.0.0 and Rails v3.2.11.

recipients = Recipient.where(category_id: 54).order('created_at DESC').take(10)
recipients.each{ |recipient| 
  @recipient_clone = recipient.clone
  @recipient_clone.category_id=63
  @recipient_clone.save
}

Aucun commentaire:

Enregistrer un commentaire