ruby-on-rails-5has-and-belongs-to-manyjbuilder

In Jbuilder, How Can you Generate an Array of IDs Without the 'ID' Key?


I have the following Jbuilder setup but I'd like to output the 'uw_question_ids' as values only. No 'id:' key. Is this possible?

json.menu do
  json.uw_question_ids menu.uw_questions do |uw_question|
    json.(uw_question, :id)
  end
end

Currently, the output JSON is...

"menu":{"uw_question_ids":[{"id":"17"}]}

I'd like it be...

"menu":{"uw_question_ids":[{"17"}]}

Solution

  • I figured it out... simple!

    json.menu do
      json.uw_question_ids product.uw_question_ids
    end
    

    I didn't realize that Rails gives you access to a 'collection_singular_ids' method (in my case, 'uw_question_ids') when you create has_many or has_and_belongs_to_many associations. That's what I needed.