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 900139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:20:03+00:00 2026-05-15T15:20:03+00:00

I need to bring an array of ruby objects in JSON. I will need

  • 0

I need to bring an array of ruby objects in JSON. I will need to find the item in the JSON object by id, so I think it is best that the id is the key of each object. This structure makes the most sense to me:

{
    "1":   {"attr1": "val1", "attr2": "val2"},
    "2":   {"attr1": "val1", "attr2": "val2"},
    "3":   {"attr1": "val1", "attr2": "val2"}
}

That way I can easily call into the json object like console.log(json_obj[id].attr1)

The issue is that I am not quite sure how to build this in ruby. This is as far as I have gotten:

# in ruby
@book_types = []
BookType.all.each do |bt|
   @book_types << {bt.id => {:attr => bt.attr}}
end
@book_types = @book_types.to_json

// In JS
var bookTypes = JSON.parse('<%=raw @book_types %>');

2 questions: How can I build this in ruby? Is there a better way to accomplish what I am doing?

Also just a note that I am building this on the Rails framework

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-05-15T15:20:04+00:00Added an answer on May 15, 2026 at 3:20 pm

    Assuming BookType is an ActiveRecord class, you can just do this:

    BookType.all(:select => "attr1, attr2").to_json
    

    …where "attr1, attr2" is a list of the attributes you want to include in your JSON.

    If you want the ids as keys, you can do this instead:

    BookType.all.inject({}) { |hsh, bt|
      hsh[bt.id] = { "attr1" => bt.attr1, "attr2" => bt.attr2 }
      hsh
    }.to_json
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a link for each depotname that will bring up a child window
I need a linq expression that will find the nearest numbers (both greater or
I need to bring .post_entry(right side) up so that it's level with .post_info(left side.
I need to have a list with id's for each list item being #one
I need to drop into C++ from C# and bring back a 2D array
I'm outputting a list of items from an array in Ruby. I need to
When I get a message I need to bring the activity to front. I
I need to either bring front browser window if it is already running or
I need to fire of a bunch of threads and would like to bring
I need to know is some object being dragged over my Delphi application. For

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.