Rails json/jbuilder - Group json view based on category - ruby-on-rails

I have an web application maded in RoR.
I need to provide the json to an mobile App.
This is my code to generate the Json:
json.category do
json.name monument.category.name
json.monumento do
json.name monument.name
json.id monument.id
json.pois monument.pois do |p|
json.name p.name
end
end
end
The output is:
{
"category": {
"name": "Igrejas",
"monumento": {
"name": "Igreja de Santa Maria do Olival",
"id": 2,
"pois": [{
"name": "Igreja de Santa Maria do Olival"
}]
}
}
},
{
"category": {
"name": "Igrejas",
"monumento": {
"name": "Igreja de São João Baptista",
"id": 3,
"pois": [{
"name": "Igreja de São João Baptista"
}]
}
}
},
{
"category": {
"name": "Igrejas",
"monumento": {
"name": "Igreja da Nossa Senhora da Graça",
"id": 4,
"pois": [{
"name": "Igreja da Nossa Senhora da Graça"
}]
}
}
},
My Problem is that i need to group the categories.
something like this:
{
"category": {
"name": "Igrejas",
"monumento": {
"name": "Igreja de Santa Maria do Olival",
"id": 2,
"pois": [{
"name": "Igreja de Santa Maria do Olival"
}]
},
"monumento": {
"name": "Igreja de São João Baptista",
"id": 3,
"pois": [{
"name": "Igreja de São João Baptista"
}]
},
"monumento": {
"name": "Igreja da Nossa Senhora da Graça",
"id": 4,
"pois": [{
"name": "Igreja da Nossa Senhora da Graça"
}]
}
}
},
Any ideas how can i solve this problem?

You need to do something like this
In your controller,
#categories = Category.where(name: 'Igrejas').includes(monuments: :pois)
In your view,
json.category #categories do |category|
json.name category.name
json.monuments category.monuments do |monument|
json.name monument.name
json.id monument.id
json.pois monument.pois do |p|
json.name p.name
end
end
end

Related

How to merge a JSON-object in Jbuilder

I am creating a rails app with Backbone/Marionette frontend.
I want to take the JSON-object below and transform it.
This is how it looks now
[{
"total_entries": 4
},
{
"entries": [{
"user": {
"id": 1,
"fullname": "Paul Paulsen"
},
"reports": [{
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}
]
},
{
"user": {
"id": 2,
"fullname": "Anna Palmgren"
},
"reports": [{
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}]
}
]
}
]
I want to make it look like this
[{
"total_entries": 4
},
{
"entries": [{
"id": 1,
"fullname": "Paul Paulsen"
}, {
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}, {
"id": 1,
"fullname": "Anna Palmgren"
}, {
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}
]
}
]
This is the Rabl file/code I use
json.array! [0,1] do |index|
if index == 0
json.total_entries #total
else
json.entries #reports.group_by(&:user) do |user, reports|
json.user user, :id, :fullname
json.reports reports do |report|
json.extract! report, :id, :name, :relation, :phone, :email
end
end
end
end
Try this ruby snippet.
x= [{
"total_entries": 4
},
{
"entries": [{
"user": {
"id": 1,
"fullname": "Paul Paulsen"
},
"reports": [{
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}
]
},
{
"user": {
"id": 2,
"fullname": "Anna Palmgren"
},
"reports": [{
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}]
}
]
}
]
x[1][:entries] = x[1][:entries].map{|p| [p[:user], p[:reports]]}.flatten

rxkotlin groupby is not working

could you please help me to group by the following json and return a hashMap in kotlin based on date with RxKotlin? is so easy with just kotlin but really stuck for Rxkotlin. thanks
val groupedTransactions = accountTransactions.transactions
?.groupBy { it.effectiveDate }
"transactions": [{
"id": "44e5b2bc484331ea24afd85ecfb212c8",
"effectiveDate": "20/07/2017",
"description": "Kaching TFR from JOHN CITIZEN<br/>xmas donation",
"amount": 12.00
}, {
"id": "1506aeeb8c3a699b1e3c87db03156428",
"effectiveDate": "20/07/2017",
"description": "Wdl ATM CBA ATM CIRCULAR QUAY STATION NSW 221092 AUS",
"amount": -200.00,
"atmId": "129382"
}, {
"id": "9a899bfd978511e9605774e1d5222b67",
"description": "Savings",
"effectiveDate": "19/07/2017",
"amount": 10.00
}, {
"id": "1a6c48627cecaa2388b702fa33d751ff",
"description": "PTAG COCA COLA AMATI",
"effectiveDate": "12/07/2017",
"amount": -2.20
}, {
"id": "7ecc19e1a0be36ba2c6f05d06b5d3058",
"description": "Wdl ATM CBA ATM TOWN HALL SQUARE NSW 253432 AUS",
"effectiveDate": "04/07/2017",
"amount": -50.00,
"atmId": "137483"
}, {
"id": "b71bf065b640217dad602f86ac047722",
"description": "BPAY - Telstra mobile",
"effectiveDate": "04/07/2017",
"amount": -49.00
},{
"id": "ef087651eb482bae4624478696f4ad4f",
"description": "Transfer from REBECCA SHAW<br/>Lorem ipsum",
"effectiveDate": "03/07/2017",
"amount": 150.00
}, {
"id": "8cd283d8b7bacc277f2bae5e26ce6d1e",
"description": "Savings",
"effectiveDate": "01/07/2017",
"amount": 200.00
}, {
"id": "04117d2d74f5331f3ee4955da27cca7a",
"effectiveDate": "28/06/2017",
"description": "Transfer - Saturday drinks",
"amount": -100.00
}, {
"id": "821ae63dbe0c573eff8b69d451fb21bc",
"effectiveDate": "21/06/2017",
"description": "Wdl ATM CBA ATM CIRCULAR QUAY STATION NSW 221092 AUS",
"amount": -200.00,
"atmId": "129382"
}]
this looks lik the rxkotlin issue. I've done it through coroutine kotlin like:
doAsync { val groupedTransactions = accountTransactions.transactions ?.groupBy { it.effectiveDate } ....

How to import ruby RSpec data

Can someone please show how to directly import ruby RSpec data . I haven't found a single answer to this after hours of research.
I'm trying to return "fruit" and "vegetable" to the method foods in the pull.rb file. That data would come from the data.rb
data.rb
let(:food_data) {
JSON.parse('[
{
"Plant": "Fruit",
"Type": "Apple"
},
{
"Plant": "Vegetable",
"Type": "Carrot"
},
{
"Plant": "Fruit",
"Type": "Orange"
},
{
"Plant": "Vegetable",
"Type": "Spinach"
}
]')
}
pull.rb
def foods
File.open('data.rb'))
[:food_data][0]
[:food_data][1]
end
You can make the data into a plain json file:
food_data.json
[
{
"Plant": "Fruit",
"Type": "Apple"
},
{
"Plant": "Vegetable",
"Type": "Carrot"
},
{
"Plant": "Fruit",
"Type": "Orange"
},
{
"Plant": "Vegetable",
"Type": "Spinach"
}
]
then load it from ruby:
require 'json'
def foods
JSON.parse(File.read("food_data.json")).first(2).map do |food|
food["Plant"]
end
end
foods # => ["Fruit", "Vegetable"]

Reverse a nested array in Ruby

I'm attempting to take a JSON API response, with nested associated resources, and reverse the associations in a Rails app.
So, imagine I get a response like this:
{
"spenders": [
{
"id": 1,
"name": "John Doe",
"accounts": [
{
"id": 1,
"name": "Account One"
},
{
"id": 2,
"name": "Account Two"
}
]
},
{
"id": 2,
"name": "Jane Doe",
"accounts": [
{
"id": 1,
"name": "Account One"
},
{
"id": 3,
"name": "Account Three"
}
]
}
]
}
My goal is to convert this into structure like this:
{
"accounts": [
{
"id": 1,
"name": "Account One",
"spenders": [
{
"id": 1,
"name": "Stephen Margheim"
},
{
"id": 2,
"name": "Greg Barendt"
}
]
},
{
"id": 2,
"name": "Account Two",
"spenders": [
{
"id": 1,
"name": "Stephen Margheim"
}
]
},
{
"id": 3,
"name": "Account Three",
"spenders": [
{
"id": 2,
"name": "Greg Barendt"
}
]
}
]
}
Now, I can do this fairly well with iteration over the hash and building a new hash:
spenders_hash = {}
accounts.each do |account|
account.spenders.each do |spender|
if spenders_hash.key? spender.id
spenders_hash[spender.id][:accounts] << account
else
spenders_hash[spender.id] = hash_from_spender_and_account(spender, account)
end
end
end
spenders_hash
def hash_from_spender_and_account(spender, account)
{
id: spender.id,
name: spender.name,
accounts: [account],
}
end
I'm hoping to find [1] a more flexible solution that isn't reliant on knowing the key names in advance and [2] hopefully more efficient.
Thoughts?

Rails - better flow control - loop

I am grabbing value data: name, uid, highschool_name, graduateschool_name like this:
def add_friends
facebook.get_connections("me", "friends", :fields => "name, id, education").each do |hash|
self.friends.where(:name => hash['name'],
:uid => hash['id'],
:highschool_name => hash['education']['school']['name'] unless hash["education"].blank?,
:graduateschool_name => hash['education']['school']['name'] unless hash["education"].blank?).
first_or_create
end
end
From an array of hash:
"education": [
{
"school": {
"id": "110703012290674",
"name": "Kunskapsgymnasiet Malmö"
},
"year": {
"id": "136328419721520",
"name": "2009"
},
"type": "High School"
},
{
"school": {
"id": "112812485399398",
"name": "Malmö University"
},
"year": {
"id": "118118634930920",
"name": "2012"
},
"concentration": [
{
"id": "104076956295773",
"name": "Computer Science"
}
],
"type": "Graduate School",
"classes": [
{
"id": "165093923542525",
"name": "Programmering",
"description": "Kursen fokuserar på metoder och tekniker vid utveckling av webbapplikationer med hjälp av HTML5."
}
]
}
],
EDIT:
This code dosent work. I would like to pick every hichschool and Graduate School from this array of hash and save it.
high_schools = response['education'].collect{|ed| ed['school']['name'] if ed['type'] == "High School" }
grad_schools = response['education'].collect{|ed| ed['school']['name'] if ed['type'] == "Graduate School" }

Resources