0

I need to search two databases in Ruby on Rails. Both are about books: name, ISBN, and price.

I need to search both at the same request. Is it possible? I have already made app which is searching one database. Here is tutorial that I followed.

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75

2 Answers2

0

I suppose you mean tables, if they are related then you can do searches both in one request.

See http://guides.rubyonrails.org/association_basics.html for examples.

The examples are about activerecord, the database ORM behind Rails. They use models about books, so you shouldn't have problems to follow the logic.

EDIT: since you say it's about different databases, here an example how to connect to two at the same time. You still need to add the realtions between the two, see the previous part for that. In the example I even use two different database files, one in memory and the other in a file, using the database engine Sqlite

class Test < ActiveRecord::Base
  establish_connection(
    :adapter  => "sqlite3",
    :database => ":memory:"
  )
end

class Test2 < ActiveRecord::Base
  establish_connection(
    :adapter  => "sqlite3",
    :database => "testing.db"
  )
  self.table_name    = :tests
end
peter
  • 41,770
  • 5
  • 64
  • 108
  • Sad to say but i meant databases :( it is task for my university. Create app wich will search for something in two seperate databases... – Piotr Dołęga Nov 16 '16 at 20:42
  • two different database scheme's ? which datatabase engine are we talking about, you'd better publish yout models and what you tried. – peter Nov 16 '16 at 20:43
0

I have personally attempted to create rails applications which connect to multiple databases simultaneously, and am disappointed to say that I do not believe this is a reasonable solution to any problem. While it can technically be accomplished, it involves forcing Rails into doing a lot of things it's simply not meant to do. Before approaching the logic you would require to search multiple models (which is something you would absolutely need), you should first put some time into overcoming the hurdle of connecting to multiple databases -- specifically if Ruby on Rails is the right tool for the job.

If you feel you truly must go forward with this approach, I recommend looking at some of the answers referenced here:

Stack Overflow: Multiple Databases in Rails

Community
  • 1
  • 1
ConnorCMcKee
  • 1,625
  • 1
  • 11
  • 23
  • Hmm, so what kind of technology would You propose for this task? I need to make app wich will just 'search two databases for records'. Just some simple API i think, and integration... I started doing it in rails because i had some experience with it and i already had configured enviroment on my PC - oh and last thing, it will be used just few times for my exam, it doesn't have to be realy effective. It just has to work. – Piotr Dołęga Nov 16 '16 at 21:14