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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:45:03+00:00 2026-06-06T11:45:03+00:00

I need a bit of help with some Ruby array jujitsu. I have the

  • 0

I need a bit of help with some Ruby array jujitsu.

I have the following array called @tasks:

[#<PivotalTracker::Story:0x007f9d6b8 @id=314, @url="http://www.pivotaltracker.com/", @created_at=#<DateTime: 2012-06-18T20:23:42+00:00 ((2456097j,73422s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=357031, @name="Test ", @description="This is the description for \"Test\"", @story_type="feature", @estimate=-1, @current_state="unstarted", @requested_by="joe jones", @owned_by=nil, @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>, #<PivotalTracker::Story:0x007f9d6b8 @id=315, @url="http://www.pivotaltracker.com/", @created_at=#<DateTime: 2012-06-18T20:25:20+00:00 ((2456097j,73520s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=357031, @name="Test 2", @description="This is the description for \"Test 2\"", @story_type="feature", @estimate=-1, @current_state="unstarted", @requested_by="joe jones", @owned_by=nil, @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>, #<PivotalTracker::Story:0x007f9d6b8 @id=316, @url="http://www.pivotaltracker.com/story/", @created_at=#<DateTime: 2012-06-18T20:25:26+00:00 ((2456097j,73526s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=357031, @name="Test 3", @description="Description for Test 3 ", @story_type="feature", @estimate=-1, @current_state="unstarted", @requested_by="joe jones", @owned_by=nil, @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>] 

My end goal is to create a JavaScript array in my .erb view with just the ID values in the above array.

I was thinking of trying something like:

var myJSArray = [<%= @tasks.each { |t| print t.id.to_s+", " } %>];

However, that obviously appends a “,” to the end of the string, which is not desirable (i.e. it returns “314, 315, 316,”. It also seems like a bit of a hack and not the right way to do it.

Any ideas on how to do this properly?

Thank you!

UPDATE: After some more research on SO, it seems I can do this in two steps:

@ids = @tasks.map { |t| t.id }

and then use that in the view with:

var myJSArray = [<%= @ids.map(&:to_s).join(", ") %>];

Not sure if this is ideal or if it could be done in a single step, though.

  • 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-06T11:45:04+00:00Added an answer on June 6, 2026 at 11:45 am
    var myJSArray = [<%= j @tasks.map(&:id).join(',') %>];
    

    Or you may prefer something like this:

    var myJSArray = <%= @tasks.map(&:id).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 bit of help with some maths. I have a range 0
I need some help converting a java byte array to a 7-Bit ASCII string.
I have the following strange situation here I need some help with: I am
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need to integrate some legacy 32-bit code - for which I don't have
Ok I need some help, I have an online calculator where I have to
i need a little bit of help with some jquery thing that i'm sure
I need some help tracking down a bit of nitty gritty information on the
A bit confused with wiring controllers, I need some help I create a new
I need little bit help related to android tabhost. I have 3 tabs and

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.