0

i have the following array of json objects.

[{"template":{"title":"title 1"}},{"template":{"title":"title 1"}},{"template":{"title":"title1111"}},{"template":{"title":""}},{"template":{"title":""}},{"template":{"title":"this is the title"}},{"template":{"title":"title232"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"this is a title"}},{"template":{"title":"this is a title"}},{"template":{"title":"this is a title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}}]

i want this array to be like

[{'title': 'some title'}, {'title': 'some other title'}, ...]

so basically i don't want the template property inside the js response. i'm getting this from the following rails code

@templates = Template.find_all_by_user_id(current_user.id, :select=>'title', :conditions=>"title is not null")

@templates.to_json is what gives me this array of json objects

any suggestions?

2 Answers 2

1

Check: http://apidock.com/rails/ActiveRecord/Serialization/to_json

Template.include_root_in_json = false
Sign up to request clarification or add additional context in comments.

Comments

0

I think that in this case, you could do with just collecting the attributes of your entire collection of Templates:

@templates = Template.find_all_by_user_id(current_user.id, :select=>'title', :conditions=>"title is not null").collect(&:attributes)

(Scroll to the right to see the added .collect(&:attributes) at the end of the line.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.