简体   繁体   中英

Use models in rails 2.3.8 for rails 4.0 api

I am writing APIs in Rails 4.0. This system is supposed to expose resources whose business logic (models) is written in Rails 2.3.8. GET requests are simple to design since we can connect to the database and expose resources easily.

However, to perform Create, Update, Delete operations, the API system needs to call the models written in another application in Rails 2.3.8. A complete re-write of models is out of scope.

How can I use the models written in Rails 2.3.8 in Rails 4.0? Is it possible to share the models between two different rails/ruby versions? Also, what patterns/design solutions exist for solving such problem?

I came up with the following solutions -

  1. Use Druby and create objects of Rails 2.3.8 models in Rails 4. Invoke methods to save etc and wrap them to get exceptions. The problem I see is that this may not be scalable because there could be multiple instances of rails 2.3.8 application running on different boxes, would we then need to start druby service on each? What if these instances are sitting behind a load-balancer?

  2. Use ZeroMQ to connect Rails 4 and Rails 2.3.8 apps. We could use the request-reply pattern to achieve synchronization. ZeroMQ would connect to the load balancer, send request to Rails 2.3.8 app and have a response sent back to Rails 4.0 app. An observer could be sitting in Rails 2.3.8 app observing any incoming request on ZeroMQ, and create objects, or capture exceptions and return response. Any suggestions for using other than observer pattern?

Are there any solutions? Which solution in better? Is there a way to share ruby code written in different versions for models?

If making HTTP requests for CRUD operations is feasible (ie performant enough) for your application—and I know that's a big if —it should be pretty easy to use Active Resource (in your Rails 4 app) to make requests against your Rails 2.3 app. Since Active Resource was designed with Rails' REST implementation in mind, you shouldn't need much more than the scaffolded controller actions in the Rails 2.3 app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM