Question

I want to have a method on a class to copy the object and all of the relationships to another database that has the same structure. How can I accomplish this?

Était-ce utile?

La solution

You could have a look at dbcharmer, which will allow to switch connections on the fly, so copying a record would be a piece of cake I guess.

[UPDATE] Small example: you do not have to mess with the ActiveRecord::Base.connection

Suppose your database.yml looks like this:

production:
  blah:
    adapter: mysql
    username: blah
    host: blah.local
    database: blah

  foo:
    adapter: mysql
    username: foo
    host: foo.local
    database: foo 

Then you should be able to do something like (disclaimer: I only read the documentation myself)

original_user = User.on_foo.find(99)

User.on_blah.create(name: original_user.name, ...)

I guess you could even do something like

User.on_blah do 
  original_user.save
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top