# Linking API derived data to my application's database

**URL:** <https://www.ruby-forum.com/t/linking-api-derived-data-to-my-applications-database/191272>\
**Category:** Rails\
**Created:** [July 8, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/linking-api-derived-data-to-my-applications-database/191272 "2010-07-08T18:05:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@mattb](https://www.ruby-forum.com/u/mattb)\
**Post date:** [July 8, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/linking-api-derived-data-to-my-applications-database/191272/1 "2010-07-08T18:05:34Z")

</div>

Hi guys,

Given a page that retrieves data from a 3rd party API, how would you  
then go about associating additional info in the application’s  
database with that record?

Specifically, the page displays info on a product, and I’d like to  
store and display locally created reviews for that product.

To display the product I’m using a route of the form /product/  
 where the productid is the barcode of the product. The  
barcode is used in the controller to look up the data via the 3rd  
party API.

It’s the structure of the database / model and how to link one the  
other that I’m struggling with conceptually. (I’m a relatively newbie  
with Rails, Ruby & ActiveRecord, and programming in general (!) so  
please forgive me if this is painfully obvious!)

Many thanks in advance for any advice or suggestions…

Matt.

---

<div class="post-metadata">

**Author:** ![mattb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@mattb](https://www.ruby-forum.com/u/mattb)\
**Post date:** [July 8, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/linking-api-derived-data-to-my-applications-database/191272/2 "2010-07-08T18:57:10Z")

</div>

MattB wrote:

> Hi guys,
> 
> Given a page that retrieves data from a 3rd party API, how would you  
> then go about associating additional info in the application’s  
> database with that record?
> 
> Specifically, the page displays info on a product, and I’d like to  
> store and display locally created reviews for that product.
> 
> To display the product I’m using a route of the form /product/  
> where the productid is the barcode of the product. The  
> barcode is used in the controller to look up the data via the 3rd  
> party API.
> 
> It’s the structure of the database / model and how to link one the  
> other that I’m struggling with conceptually. (I’m a relatively newbie  
> with Rails, Ruby & ActiveRecord, and programming in general (!) so  
> please forgive me if this is painfully obvious!)
> 
> Many thanks in advance for any advice or suggestions…
> 
> Matt.

Seems like you’ve answered your own question. I’d keep it simple. Make  
the barcode value a uniquely-indexed attribute of your own product  
record (with it’s id field). Use the id field for all your rails coding

- the product record and parent id of the child review records, and use  
the barcode value to access the external 3rd party API data.

---

<div class="post-metadata">

**Author:** ![mattb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@mattb](https://www.ruby-forum.com/u/mattb)\
**Post date:** [July 8, 2010, 8:51pm UTC](https://www.ruby-forum.com/t/linking-api-derived-data-to-my-applications-database/191272/3 "2010-07-08T20:51:17Z")

</div>

On Jul 8, 5:57 pm, Ar Chron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Use the id field for all your rails coding
> 
> - the product record and parent id of the child review records, and use  
> the barcode value to access the external 3rd party API data.

Hi Ar,

That’s pretty much how I’d originally structured it when I was using  
all locally derived data - a products table and a reviews table, along  
the lines of the posts / comments example in the Getting Started with  
Rails Guide, with the barcode stored in the products table, in turn  
used to look up item attributes from the API.

So: /products//reviews/ with referencing the database id  
field. All fine so far.

However, now I have a model-less /products/ that can display  
any item, for which all data is coming from the API, and for which  
there’s no product record, may or may not have reviews, ie. It’s  
possible to search for and display data for _any_ product that the API  
has data for, with no local database involved, hence I don’t have a  
product\_id field to code around.

That’s where I’m struggling (and probably mostly because I’m coding by  
example and haven’t found a suitable example to borrow from).

Thanks,  
Matt.
