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.
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.
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
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: