0

I'm wondering if there's an efficient and elegant way to 'link' an activeresource object with an activerecord object.

I'm installing a payment system for my application, it works with an API that contains user table, wallet table, ... I want to store all this information in my application database. for eg: - when I create a wallet_object_api_side, I want to store it in wallet_object_my_database - when I update wallet_object_api_side, I want to update wallet_object_my_database

wallet_object_api_side MUST ALWAYS BE EQUAL TO wallet_object_my_database

Is there an elegant way to do both creating the api object and saving it in my database in a same request ?

Thank you.

1 Answers1

0

You can do this with Rails callbacks.

But the bigger question is why are you trying to do this? It's dangerous to rely on replicating the remote data locally and vice versa. If the API is the definitive source of the data, then you should go to the API when you need the data.

What if something changes in the API database; how is your application going to know about it and deal with it?

John Feltz
  • 63
  • 5
  • The payment API is the definitive source of the data. The changes in the API database are strictly dependant of my application calls; so I imagined to store everything locally. The only reason why I want to store locally the data is to avoid to load API resource and local resource for similar objects ( eg: first_name is asked by API for user registration, I also have in my database because users are not always "api registred".. how can I deal with that ?) And also, I wanted to avoid to load API resource each time I refresh a page (is it a good way?). Thank you for your recommendations. – Amaury B - France Oct 17 '14 at 18:21
  • I think you have a number of things going on here. To answer your initial question, use callbacks. But I don't understand why you are designing the architecture you have - the whole point of an API is for fast and efficient storage and retrieval of data. If you have one, use it! – John Feltz Oct 17 '14 at 18:49