Search code examples
ruby-on-railspaperclipruby-on-rails-2

extract attributes values from an activerecord query object in rails 2.3


In my controller I have a @attach object and when I inspect it, it has values as

[#<MessageAddlAttachment id: 80, reminder_id: 112, msg_attachment_file_name: "24.png", msg_attachment_content_type: "image/png", msg_attachment_file_size: 272368, created_at: "2013-10-10 12:04:37", updated_at: "2013-10-10 12:04:37">, #<MessageAddlAttachment id: 81, reminder_id: 112, msg_attachment_file_name: "37.png", msg_attachment_content_type: "image/png", msg_attachment_file_size: 333986, created_at: "2013-10-10 12:04:37", updated_at: "2013-10-10 12:04:37">]

So now after some operation I need to create an entry in this MessageAddlAttachment table with different ids. How can I achieve it. I tried dup but it will have same ids. Please help


Solution

  • dup is your friend in rails 4. it will create a copy but removes the id value:

    u = User.first
    => #<User id: 1, ...>
    u.dup
    => #<User id: nil, ...>
    u.dup.save
       (0.2ms)  begin transaction
    ...