Relating to my last question here: Rails: Finding all associated objects to a parent object
Is it possible to sort multiple separate child objects in Rails by creation date, and then list them? Using the previous example I have a resume with two different has_many child objects, I would like to fetch them and then sort them based on creation date and then use that to display them.
I assume that you have two (or more) seperate models for children objects, so your
Parentmodel looks like this:To sort them and get them generally as
childrenyou can write method (similar to @gertas answer):and put it in
Parentmodel. Then you can use it in controller:Now we’ll try to display them in a view. I assume that you have created two partials for each model
_cat.html.erband_dog.html.erb. In view:It should automaticaly find which partial should be used, but it can be used only if you follow Rails way. If you want to name partials in different way, or store it in different directory, then you would have to write your own methods that will choose correct partial based on type od object.