0

How can a json id and database table id be mapped in ruby on rails and the output of the result be shown on UI. I have a collection of entries in my database say for id, name, url, code, and on UI i have implemented a table whose values are from Json field. I need to fetch data from database for url and code on UI using map concepts in rails.

Example : Json field : [position":"1","name":"Bitcoin","change24":"-2.59 %","currency":"usd","id":"btc"]. My database has url and id, where id is the same value as in json. All thats needed is to map the id in-order to get the corresponding url on UI.

3
  • Could you be more specific, may give an example? What are your input values and what would you expect as an output? Commented Mar 11, 2014 at 7:39
  • Have edited the content. Commented Mar 11, 2014 at 7:48
  • Add a helper method, use it in UI, pass the JSON ID to get the URL from DB. please add some code samples that you have tried, so that its easy to sort out. Commented Mar 11, 2014 at 7:55

1 Answer 1

1

So is this something you are going for?

@json_data.map! do |json_item|
  db_item = @database_data.detect{|dbi| dbi['id']==json_item['id']}
  json_item['url'] = db_item['url'] if db_item
end
Sign up to request clarification or add additional context in comments.

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.