Share Models between 2 Rails API's (Separate Applications)
You could take a look at:
- Best way to share ActiveRecord models and data between different Rails Apps?
- Two rails apps sharing a model folder
- Shared models between two Rails apps - what is the ideal solution for Workflow?
Share common models between two Rails Application
None of these.
How about creating a shared gem and put that on your private git repo? With Rails, you can provide the same mechanism for autoloading the models within the application itself by packaging the gem as a rails_engine
See example
At the end require it within Gemfile
gem 'my_gem_name', git: 'http:my_git_repo.com/my_gem_name'
A more advance solution would be creating a REST service for the desired data in the e-commerce app and consume it in the admin app. Especially for Users (and authentication), it would even make sense to extract it to a separate app and provide a UserAuthentication Service.
How to share data between two Rails apps in both ways using rest-client in rails 4
So App2 is kinda front-end app for App1. To update App1 records from App2 you need to rewrite update
action in PremiumListingsController
. It shouldn't update listings in App2 DB, but instead it should send PUT
request to App1 with current listing properties. After getting a response it should redirect to @premium_listing
with success message or render show
with error message (this is what it does now).
It should look like so:
def update
property_uri = "#{API_BASE_URL}/properties/" + params[:id]
property_rest_resource = RestClient::Resource.new(property_uri, :headers => {:Authorization => FULL_AUTH_FIELD})
property_rest_resource.put(premium_listing_params)
format.html { redirect_to @premium_listing, notice: 'Premium listing was successfully updated.' }
format.json { render :show, status: :ok, location: @premium_listing }
rescue => e
format.html { render :edit }
format.json { render json: @premium_listing.errors, status: :unprocessable_entity }
end
(This is just a sketch to demonstrate the idea, not sure if it works)
App1 should have update
action for /properties/:id
url. This action takes parameters, updates the listing in DB and returns 200/204
code for success or 4**
code for errors. RestClient raises an exception when response code is not 2**
.
Best way to share ActiveRecord models and data between different Rails Apps?
I'm using a shared database to solve the shared data problem between heroku and a server on EC2 which does some background processing I can't do on heroku. In my case I'm using the same application, but really all I'm doing is using delayed_job on the EC2, so I'm only using the models from the app there. If I were going to use them in another application, I would probably go through the trouble of creating a gem to use between them.
Sharing Models between Rails App and Ruby Script
If you make the controllers available through JSON, you will be able to access them easily over the internet.
http://localhost:3000/people.json
[
{"id":1,
"name":"Eva Smith",
"age":42,
"created_at":"2017-02-20T18:52:38.414Z",
"updated_at":"2017-02-20T18:53:03.112Z",
"url":"http://localhost:3000/people/1.json"},
{"id":2,
"name":"John Smith",
"age":32,
"created_at":"2017-02-20T18:52:55.463Z",
"updated_at":"2017-02-20T18:52:55.463Z",
"url":"http://localhost:3000/people/2.json"}
]
You can parse this on the Ruby end and turn it into an array of objects.
JSON.parse(json_string, object_class: OpenStruct)
Exchange data between 2 Rails apps (preferably offline) on the same server
I had the same problem and explored every option: ActiveResource (which is deprecated), callbacks with my own homegrown API wrappers, queueing with Redis or RabbitMQ. Nothing was easy enough for my simple mind to implement. If Model1
in App1
will always update Model2
in App2
, then best solution I've found with Rails is the Promiscuous gem
It's makes it pretty simple to run a pub/sub system that keeps data synched up between two ruby/rails apps. It works with both ActiveRecord & Mongoid. The documentation goes into more depth, but here are a few gotchas I found when trying to set it up using the Quick Start guide on the github page.
- Make sure you have an initializer file in both apps that connects to your shared RabbitMQ instance.
If using ActiveRecord on the publisher side, you will need to create a new table (subscriber does not need this table to my knowledge):
create_table :_promiscuous do |t|
t.string :batch
t.timestamp :at, :default => :now
endYou will also need to add a column to every publisher and subscriber model
# in App1 - publisher
add_column :publisher_model, :_v, :integer, limit: 8, default: 1
# in App2 - subscriber
add_column :subscriber_model, :_v, :integer, limit: 8You can set the name of the published model. For example if I have a namespaced class
Admin::User
in App1, I can publish the attributes:as => 'AdminUser'
and App2 has the modelAdminUser
it will listen correctly.- If you've followed the instructions from the github page, included your mixins and set publishable/subscribable attributes, you will inevitably want to run it in production in which case your subscriber will need to run a worker. I use a pretty shameless ripoff of this Resque deploy script, my version for Promiscuous can be found here and it seems to work.
I'm finding more and more ways to use this setup. Gives me a lot more flexibility with sharing and managing my data. Good luck.
Related Topics
How to Organize Minitest/Unit Tests
Rake Assets:Precompile Gets Killed When There Is a Console Session Open in Production
How to Split a String into Only Two Parts, by the Last Occurrence of the Split Char
Vi Input Mode in Command Line Matlab
Running Multiple Background Parallel Jobs with Rails
Why Is Date.Today - 6.Months + 6.Months != Date.Today
Rails: Why Images Are Not Showing in My Rails Basic App
Ruby Model Output Id as Object Oid
Converting Nested Hash Keys from Camelcase to Snake_Case in Ruby
How to Find a Devise User by It's Session Id
Ruby: How to Group a Ruby Array
How to Validate the Presence of a Belongs to Association with Rails
If Else Statements in .Html.Erb in Views
What Is the Community-Preferred Ruby Unit Testing Framework
Star Rating in Ajax with Ruby on Rails
How to Alias a Class Method in Rails Model
Ruby/Ror: Calling Original Method via Super()
How to Profile Ruby 1.9.2 Scripts with Memory Allocation Reports