0

I have some models in my Rails application that I would like to also use in a Ruby script, that are deployed on separate VM's.

I could create a gem that includes these models, and then require this gem from both apps. I could also link the repositories in github so that the models from the rails app get deployed with the Ruby script.

I feel like the gem approach might work, but not sure what the best practice is here.

Are there any other options?

Thanks

3
  • Can you expand on how the models would be used in a standalone Ruby script? Are you referring to just the object model, or do you also require the ability to interact with existing models in your DB? Commented Feb 20, 2017 at 19:01
  • It is mostly for db interactions. So just exposing/consuming the API makes the most sense for this. Commented Feb 20, 2017 at 19:07
  • Gotcha. I'd note that depending on your deployment configuration and specific script requirements it may make more sense to deploy your rails app to the other systems and implement your scripts as rake tasks vs running a ruby script that interacts with another server. Commented Feb 21, 2017 at 1:39

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

I thought I'd expand on my answer. Knowing now that the question is how to use records that exist on your server in other scripts, there's essentially two choices. Do you do this in the app, or out?

Run the Scripts as Rake Tasks

Load a copy of the app into the VMs in question and run the script as a rake task. The benefits of doing this are straightforward:

  • Tighter integration between script and model logic.
  • Changing APIs is far easier when you only have to manage one code base.
  • Code-sharing (model methods, helpers, etc)
  • No dependency on an additional Rails server.

Drawbacks:

  • VM resources required by by the deployment process (gem install, spin-up, etc) may not be practical.
  • Requires ability to deploy changes to the VMs concurrent with server changes.

Expose the Records with an API

The benefits are pretty much the inverse of the above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.