locomotivecms/engine — Issue #317: has_many associations don't follow the sort_by clause 
Author: kikito
Created: 2012-03-06 15:47:57+00:00
State: open

Hi again!

My (tireless) users have detected a bug in Locomotive.

The issue is the following: order_by clause is ignored in has_many relationships (I believe creation date is used in all cases there).

Simple example with two content types (in pseudo-code):

Category:
name: string
news: has_many(News) inverse category

News:
title: string
date: date
category: belongs_to(Category)
sort_by: date

Notice that news are marked as "sorted by Date".

Say I have 6 news - by date they are N1,N2,N3,N4,N5,N6 but by *creation date* they are N5,N2,N3,N1,N6,N4,

If I do this:

{% for news in contents.news %}

{{ news.title }} - {{ news.date }}

{% endfor %}

I get the news sorted out by the Date field (as expected).

* N1
* N2
* N3
* N4
* N5
* N6

However, when I group by category:

{% for category in contents.category %}

{{ category.name }}

{% for news in category.news %}

{{ news.title }} - {{ news.date }}

{% endfor %}

{% endfor %}

I get the news sorted out by insertion date, not by the Date field:

* Foo
* N2
* N3
* N1
* Bar
* N5
* N6
* N4

Expected result (sorted by Date):

* Foo
* N1
* N2
* N3
* Bar
* N4
* N5
* N6

I'm pretty sure that the fix must be around app/models/content_instance and/or the custom_fields plugin. But that's all I could deduce; I wasn't able to find where exactly the "has_many relationships" get transformed into collections.