开发者

How to get data from Another Database into My Database?

RoR = 2.3.11 Ruby = 1.8.7 Gem = 1.3.7

I have one database (call it "First") with 4 tables. And I Have another database (call this database "Second"), (w开发者_StackOverflow社区ith another password). I want take emails (field) from Second and table if there are any updates was and inserting into First database.

Question: How I can do it?


You can define different databases in your database.yml.

first:
  adapter: mysql
  database: first_development
  username: user
  password: pwd
  host: localhost

second:
  adapter: mysql
  database: second_development
  username: user
  password: pwd
  host: localhost

and then connect your models to different databases using ActiveRecord::Base.establish_connection

class A < ActiveRecord::Base

  ActiveRecord::Base.establish_connection "first"

end

class B < ActiveRecord::Base

  ActiveRecord::Base.establish_connection "second"

  def self.sync
    A.all.each do |record|
     B.create(:email => record.email)
    end
  end

end

I added a simple method called sync that can be a starting point for your synchronisation issue,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜