Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:03:25+00:00 2026-06-17T23:03:25+00:00

I have a controller that renders json. Here’s code: class AppLaunchDataController < ApiController def

  • 0

I have a controller that renders json. Here’s code:

class AppLaunchDataController < ApiController
    def index
        service_types = []
        vendors = []
        tariffs = []
        fields = []
        vendors_hash = {}
        service_types_hash = {}
        tariffs_hash = {}
        fields_hash = {}

        @service_types = ServiceType.select("title, id").all.each do |service_type|
            service_types_hash = {id: service_type.id, title: service_type.title}
            service_types << service_types_hash
            @vendors = service_type.vendors.select("title, id").all.each do |vendor|
                vendors_hash = {id: vendor.id, title: vendor.title}
                vendors << vendors_hash
                @tariff = vendor.tariffs.select("title, id").all.each do |tariff|
                    tariffs_hash = {id: tariff.id, title: tariff.title}
                    tariffs << tariffs_hash
                    @fields  = tariff.fields.select("id, current_value, value_list").all.each do |field|
                        fields_hash = {id: field.id, current_value: field.current_value, value_list: field.value_list}
                        fields << fields_hash
                    end
                    tariffs_hash[:fields] = fields
                    fields = []
                end
                vendors_hash[:tariffs] = tariffs
                tariffs = []
            end
            service_types_hash[:vendors] = vendors
            vendors = []
        end
        render json: service_types
    end
end

Return value looks like this:

[{"id":1,"title":"Water",
"vendors":[{"id":1,"title":"Vendor_1",
"tariffs":[{"id":1,"title":"Unlim",
"fields":[{"id":1,"current_value":"200","value_list":null},{"id":2,"current_value":"Value_1","value_list":"Value_1, Value_2, Value_3"}]},{"id":2,"title":"Volume",
"fields":[]}]},
{"id":2,"title":"Vendor_2",
"tariffs":[]}]},
{"id":2,"title":"Gas",
"vendors":[]},
{"id":3,"title":"Internet",
"vendors":[]}]

It works, but I’m sure there’s another (more rails-) way to get the result.
If anyone dealt with it before, please help. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:03:26+00:00Added an answer on June 17, 2026 at 11:03 pm

    just use

    # for eager-loading :
    @service_types = ServiceType.includes( vendors: {tariffs: :fields} ) 
    # now for the json :
    @service_types.to_json( include: {vendors: {include: {tariffs: { include: :fields}}}} )
    

    if your ServiceType object will always have this kind of representation, just override the model’s as_json method:

    class ServiceType
      def as_json( options={} )
        super( {include: :vendors }.merge(options) ) # vendors, etc.
      end
    end
    

    this is encouraged way to do it in rails : calling to_json on the model will just call as_json, possibly with additional options. In fact, as_json describes the canonical json representation for this model. See the api dock on to_json for more insight.

    If your needs are more peculiar ( as using selects for a faster query ), you can always roll your own to_json_for_app_launch_data method on the model (using or not as_json), or even better on a presenter

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller that looks like this: public class PageController : Controller {
I have a controller that is called with AJAX (sends JSON data), so I
I would like to test a controller that directly renders some JSON output (by
In my controller action, I have the following: def index @articles = (params[:mine] ==
Here I have a piece of code that should work based on what I
I have a controller that runs some calculation on data, and then I need
I have a controller that contains a tableView that spawns another controller modally to
--Summary (shortened)-- I have a controller that loads a profile object from the corresponding
When you have a controller that does logic with services and DAO's that may
I have a bit of a strange problem. I have a controller that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.