The main idea is that I have several worker instances of a Rails app, and then a main aggregate
I want to do something like this with the following pseudo pseudo-code
posts = Post.all.to_json( :include => { :comments => { :include => :blah } })
# send data to another, identical, exactly the same Rails app
# ...
# Fast forward to the separate but identical Rails app:
# ...
# remote_posts is the posts results from the first Rails app
posts = JSON.parse(remote_posts)
posts.each do |post|
p = Post.new
p = post
p.save
end
I'm shying away from Active Resource because I have thousands of records to create, which would mean thousands of requests for each record. Unless there is a way to do it all in one request with Active Resource that is simple, I'd like to avoid it.
There are several options you could implement to get this to work:
As others have answered, you could make use of ActiveResource. After reading your comments, this seems like a solution you'd like to steer clear of due to the multiple-request aspect
You could have a controller in your second rails application that receives data and creates records out of it.
class RecordReceiver < ActiveRecord::Base
def create
params[:data][:posts].each do |p|
Post.create(p)
end
end
end
You could namespace this controller inside an "API" namespace, which is a rather clean solution if implemented properly.
You could share one database across two applications. This means you won't need to send the data from one model to another, it will already be there. This is the least amount of work for you as a developer, but may not be possible depending on the system architecture you have.
You could implement multiple database in each application, like so:
#Add to database.yml
other_development:
adapter: mysql
database: otherdb_development
username: root
password:
host: localhost
other_production:
adapter: mysql
database: otherdb_production
username: root
password:
host: localhost
Then, define your models like so:
class Post < ActiveRecord::Base
end
class PostClone < ActiveRecord::Base
establish_connection "other_#{RAILS_ENV}"
end
Now, your Clone
model will point to the current database, and the PostClone
model will point to the other database. With access to both, you can copy the data over whenever you need to with basic model methods.
Since you don't want to use ActiveResource, I would recommend that you simply share the database between the applications. If this isn't a possibility, then try having two models, each going to a different database. Finally, the receiving controller is a valid, albeit slower option (as it needs to do the HTTP request on top of the database requests)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With