Order by in association loading

0 views
Skip to first unread message

Sergio Viudes

unread,
Jul 23, 2009, 4:06:33 PM7/23/09
to JazzRecord
Hi, first of all sorry for my bad english, I'm from Spain!

I would like to know if it's possible to define how to order loaded
associated records.
In my config I set JazzRecord.depth to 1, so 1 level is auto-loaded.

I modified your code to be able to do this in model:

MyModel = new JazzRecord.Model({
table: "table",
columns: {
c1: "text",
c2: "number"
},
defaultOrder: "c1"
});

So I defined that records of table "table" are ordered (by default) by
"c1" column.

I modified these two lines of JazzRecord.Model.prototype.select
function to work like expected:

if(options.order || this.options.defaultOrder)
options.order = "ORDER BY " + (options.order ? options.order :
this.options.defaultOrder);

It would be nice if you can include something like that in your code.
Or maybe something to luse like this:
MyModel.associatedRecords.orderBy(field);

Thanks!, JazzRecords is awesome!!

Nick Carter

unread,
Jul 24, 2009, 6:26:36 AM7/24/09
to JazzRecord
Hi Sergio, and thanks for the interest in JazzRecord!

This is a good idea, one supported in Rails' ActiveRecord as well.
Since it is something derived from ActiveRecord, I've tried to stick
with the naming convention, which is to simply call it "order". I've
added this as an optional property in Model declarations. See
example_models.js, example_fixtures.js, example_migrations.js in the
latest code in the 0.7 branch of the repo here:
http://github.com/thynctank/jazzrecord/tree/2bd4775be0d73578f9a684056644228e2c05aaf8/javascripts/examples

Note that this only works when querying the model directly.
Autoloading is something that needs to be looked at more closely. The
code that currently builds the queries for autoloading is somewhat
convoluted. It has some ideas to be used in async JazzRecord which may
or may not ever be used, so I may end up scrapping the current code
(yet again) and building something more sophisticated that can take
into account future ideas along the lines of this default order.
Hopefully I'll find time to do this over the weekend.

Again, thanks for the interest, thanks for the ideas (especially
thanks for actually looking over the source and taking the time to
understand it). Your English is fine!

Sergio Viudes

unread,
Jul 24, 2009, 6:39:09 AM7/24/09
to JazzRecord
Thanks for your reply and for your work!

On 24 jul, 12:26, Nick Carter <thynct...@gmail.com> wrote:
> Hi Sergio, and thanks for the interest in JazzRecord!
>
> This is a good idea, one supported in Rails' ActiveRecord as well.
> Since it is something derived from ActiveRecord, I've tried to stick
> with the naming convention, which is to simply call it "order". I've
> added this as an optional property in Model declarations. See
> example_models.js, example_fixtures.js, example_migrations.js in the
> latest code in the 0.7 branch of the repo here:http://github.com/thynctank/jazzrecord/tree/2bd4775be0d73578f9a684056...
Reply all
Reply to author
Forward
0 new messages