2

I have a problem with copying database records. I have a simple model User, that contains one-to-many relation with Language model and many-to-many relation with Skill model. I wanted to use amoeba gem to copy records with all associations. One-to-many copying works fine, but many-to-many doesn't copy at all.

Here's the code of User and Skill model:

user.rb

class User < ActiveRecord::Base
  belongs_to :language
  has_and_belongs_to_many :skills

    validates_presence_of :email, :name
    validates :email, 
        presence: { with: true, message: "cannot be empty" }, 
        uniqueness: { with: true, message: "already exists in database" }

  amoeba do
    enable
  end
end

skill.rb

class Skill < ActiveRecord::Base
  has_and_belongs_to_many :users
end

I have also migration files that crates users, skills and skills_users tables:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name, null: false
      t.string :email, null: false
      t.references :language
      t.timestamps null: false
    end
  end
end

.

class CreateSkills < ActiveRecord::Migration
  def change
    create_table :skills do |t|
      t.string :name
      t.timestamps null: false
    end
  end
end

.

class AddUsersSkillsTable < ActiveRecord::Migration
  def change
    create_table 'skills_users', :id => false do |t|
      t.column :user_id, :integer
      t.column :skill_id, :integer
    end
  end
end

Controller action 'show' in users_controller looks like this:

def copy
    @user_copy = @user.dup
    if @user_copy.save(validate: false)
        redirect_to action: "index"
    end
end

I tried copying relations in user.rb like this, but it didnt work:

amoeba do
    enable
    clone [:skills]
end

What may cause the problem?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338

1 Answers1

2

Ok, I found the mistake. I wrote

@user_copy = @user.dup 

in the controller file instead of

@user_copy = @user.amoeba_dup
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338