1

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"), (with 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?

1 Answer 1

5

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,

Sign up to request clarification or add additional context in comments.

7 Comments

perfect, now i understand how it works, thank you! But one esle question. for example in model (Class A) i should create some def where i grab information from database? P.S - in database.yml. If my second database is in production mode only, so i can just add like you wrote, but instead of second write my_database, yes?
See my updated post for an example on how a method could look like (without knowing exactly what your model(s) looks like). I don´t understand the second part of your question.
thank you, i will be testing now. Second part of my question, sorry. I meant, If my second (database called uzez, where table managers with email field) is in production mode on another host, so i just replace this " database: second_development" to "database: uzez_production" ? SO , Class A = Respondents 9with email field), and class B (another database with table managers with field email). yes? And last question, this method sync I should somewhere esle write?
Yes, you have to change the name of the databases and the hostname to your needs. If one of your databases is on another host, make sure it can be accessed from the machine your application is running on. P.S.: If you think my anwser is useful, upvote would be great ;-)
Yes, i your answer is useful, i will upvote (at this moment i doesn't have account i cant vote, but i will do after work). I have last question, i added everything in my model in database.yml, so question is: When/where/how i can see my data in my database from another? Know if i click on refresh button my projects nothing happens!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.