I am trying to render an object as json, including nested attributes and sort them by created_at attribute.
I’m doing this using the code:
format.json { render :json => @customer, :include => :calls}
How can I sort calls by created_at attribute?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
If you think how Rails works, calls is just a method that relates to the Call model. There are a few ways you can do this. One is to set the order option on the association. One is to change the default scope of the Call model globally, another creates a new method in the Customer model that returns the calls (useful if you wish to do anything with the calls before encoding.)
Method 1:
UPDATE
For rails 4 and above use:
Method 2 :
Method 3:
Then you can use: