I have a list of shows in a database that need to be output in a certain JSON style in order to work with Polymaps.
Part of this includes the need to iterate over one section in order to create a list of points. I'm pretty certain that this needs to be achieved using :include in the render :json => #results bit of the code.
Here's the code as it stands:
def gigs
#gigs = Show.where(:displayname => "Vans Warped Tour 2011")
#giggage = [{
:type => "FeatureCollection",
:features => [
#gigs.each do |gig|
:type => "feature",
:geometry => {
:coordinates => [
gig['lng'],
gig['lat']
],
:type => "Point"
},
:properties => gig
end
]
}]
render :json => #giggage
end
There's an each loop inside a hash which I know you can't do, but that's the best way to illustrate what I'm going for, I'm going in circles on this.
I did try this which got me some of the way there, but only returned the one result because of the structure of the loop:
def gigs
#gigs = Show.where(:displayname => "Vans Warped Tour 2011")
#gigs.each do |gig|
#gigs_to_render = {
:type => "FeatureCollection",
:features => [
:type => "feature",
:geometry => {
:coordinates => [
gig['lng'],
gig['lat']
],
:type => "Point"
},
:properties => gig
]
}
end
render :json => #gigs_to_render
end
Thanks for your help! Anyone. Everyone!
The code as it stands should be very close to working. Just change each to map and surround the body of the block in curlies so it all gets returned as a hash for each gig.
Related
I am using Fusionchart Multi-series column 3d + line with dual y axis,
I put my data in an array like this
mttr.push({
:label => Date::MONTHNAMES[month],
:value => (monthly_failure[month] / total_circuit)
})
and the dataset field like this
:dataset => [
{
:seriesname => 'Downtime',
:color => 'AFD8F8',
:showvalues => '0',
:data => downtime
},
{
:seriesname => 'MTTR',
:color => 'F6BD0F',
:showvalues => '0',
:data => mttr
},
{
:seriesname => 'SLA',
:color => '8BBA00',
:showvalues => '0',
:parentyaxis => 'S',
:renderas => 'Line',
:data => sla
}
]
I have tried my code and there is no problem with the data, but the graph still blank. is it becacuse i am not using the "categories" field ?
For implementing multi series charts you need to have categories objects as they are used to specify x axis labels for multi series charts.
Also note that FusionCharts have dedicated Ruby on Rails wrapper.
You can check the documentation from here http://www.fusioncharts.com/dev/using-with-server-side-languages/ruby-on-rails/introduction.html
I did the following query on my model:
output = user.interests.includes([:culture, :sports])
This gives me all the "interests" with all the "culture" and "sports" entries of the user.
I'd like to forward only the id column for "interests", "culture" and "sports" to the client as json.
I tried doing it this way:
output.to_json(:include => [:culture, :sports], :only => ['id'])
When doing it that way it only shows the IDs of the interests but still includes every column of "culture" and "sports". What do I have to add to restrict "culture" and "sports" also to only the IDs?
Thanks in advance!
You can add options for each of the includes by using a separate hash, e.g:
output.to_json(:include => { :culture => { :only => :id }, :sports => { :only => :id } }, :only => ['id'])
If I create a child node in RABL using the node() method, how can I control the attributes that are presented?
The JSON output is this:
[
{
"location": {
"latitude": 33333,
"longitude": 44444,
"address": "xxxxxxx",
"title": "yyyy",
"url": "http://www.google.com",
"rate": {
"created_at": "2012-09-02T11:13:13Z",
"id": 1,
"location_id": 1,
"pair": "zzzzzz",
"updated_at": "2012-09-02T12:55:28Z",
"value": 1.5643
}
}
}
]
I want to get rid of the created_at, updated_at and location_id attributes.
I have this in my view file:
collection #locations
attributes :latitude, :longitude, :address, :title, :url
node (:rate) do
|location| location.rates.where(:pair => #pair).first
end
I tried using a partial and the 'extend' method, but it totally screwed things up. Also, I tried adding attributes to the block but it didn't work (the output was as specified in the attributes but it didn't show the values for each attribute).
Thanks!
Your code: location.rates.where(:pair => #pair).first returns the whole Rate object. If you want specific fields (for example: all, except for create_at, updated_at, etc.) then you have two options:
Manually describe the hash in node():
node (:rate) do |location|
loc = location.rates.where(:pair => #pair).first
{ :pair => loc.pair, :value => loc.value, etc... }
end
Or you can do this:
node (:rate) do |location|
location.rates.where(:pair => #pair).select('pair, value, etc...').first
end
...and as a side note, I should say that placing logic (rates.where) in your view is not a best practice. see if your controller can do that for the view using the Rate model.
You wouldn't be able to use attributes within the node block, since "self" in there is still the root object or collection, so in your case #locations. See also RABL wiki: Tips and tricks (When to use Child and Node)
In the node block you could simply create your custom response by only listing the attributes that your interested in:
node :rate do |location|
rate = location.rates.where(:pair => #pair).first
{:id => rate.id, :location_id => rate.location_id, :value => rate.value}
end
You can also try the approach using a partial:
In app/views/rates/show.json.rabl
object #rate
attributes :id, :location_id, :value
Then in your #locations rabl view:
node :rate do |location|
rate = location.rates.where(:pair => #pair).first
partial("rates/show", :object => rate)
end
Is it possible to get the Unique values of a Hash? Lets say I have a hash which one of the keys is "name" Name can come up a couple dozen times, but it will only have lets say 10 variations of itself all unique. What I want to do is be able to take those 10 unique names and build a view based off of them. But in order to roll the view out properly I would need to know those unique names. Im thinking I have to build an array, or another hash or something of the unique names. So I can go over that array with "each" then for each unique if the original hash has a match apply it to that section or block on the view. Hopefully this makes sense to someone. Who can help me out.
val = {
:status => "successful",
:service_list => [
{
:service_name => "oozie",
:status => "RUNNING",
:status_message => "Running Master Service",
:host => "1"
},
{
:service_name => "single-namenode",
:status => "RUNNING",
:status_message => "Running Service",
:host => "1"
},
{
:service_name => "single-database",
:status => "RUNNING",
:status_message => "Running Service",
:host => "1"
}
]}
truncated version of the hash I am working with.
Not sure if it is the right answer, but the list of unique service names can be obtained like this:
val[:service_list].map { |service| service[:service_name] }.uniq
# ["oozie", "single-namenode", "single-database"]
Update:
Iterating over the hash of services grouped by service_name is a little easier, here's an example:
In the action of your controller:
#services = val[:service_list].group_by { |service| service[:service_name] }
# {
# "oozie" => [ { ... }, { ... } ],
# "single-namenode" => [ { ... }, { ... } ],
# "single-database" => [ { ... }, { ... } ]
# }
In your view
<% for name, services in #services %>
All services with name <%= name %>
<% for service in services %>
<%= service[:host] %>, Status: <%= service[:status] %>
<% end %>
<% end %>
Hope that helps
Consider a PersonController which has a list action. A user can choose to list all people, or only males or females; currently, to do that, they'd have to go to /people/m or /people/f, corresponding to the route
map.list_people "people/:type",
:conditions => { :method => :get },
:requirements => { :type => /a|m|f/ },
:defaults => { :type => 'a' }
(/people/a works the same as just /people/, and lists all people).
I want to change my routing so that I could have two routes, /males/ and /females/ (instead of people/:type), both of which would go to PersonController#list (DRY -- aside from an extra parameter to what's being searched, everything else is identical), but will inherently set the type -- is there a way to do this?
map.with_options(:controller => "people", :action => "index") do |people|
people.males 'males', :type => "m"
people.females 'females', :type => "f"
end
Then you should be able to do males_path or males_url to get the path for this, and I'm sure you can guess what you do to get the path to females.