Creating associated records

3 views
Skip to first unread message

Sergio Viudes

unread,
Feb 5, 2010, 2:27:24 AM2/5/10
to JazzRecord
Hello and sorry for my bad english (I'm from Spain). I want to know
how to create a record with their associated records and save all
together to database.

In my model I wrote something like this:

Table1 = new JazzRecord.Model({
table: "parent",
foreignKey: "parent_id",
hasMany: {childs: "child"},
columns: {
table1Column: "text"
}

Table2 = new JazzRecord.Model({
table: "child",
belongsTo: {parent: "parent"},
columns: {
table2Column: "text",
parent_id: "number"
}

To create a "parent" record with "child" records and save all together
should I do something like this?:

//Create parent record
var record = Table1.newRecord({
table1Column: "someText"
});

//Create childs array
record.childs = new Array();

//Create child #1
record.childs.push(Table2.newRecord({
table2Column: "someText",
parent_id: record.id
});

//Create child #2
record.childs.push(Table2.newRecord({
table2Column: "anotherText",
parent_id: record.id
});

//Save all togegher to dataBase
record.save();

That's the correct way to do it?

Nick Carter

unread,
Feb 7, 2010, 9:57:04 PM2/7/10
to JazzRecord
No problem about the English!

That's almost correct, but a few close parentheses are missing, and
you need to be sure to call JazzRecord.migrate() after declaring the
models. Also, a base record needs to be saved before new associated
records will save correctly. An updated version of the code follows:

Table1 = new JazzRecord.Model({
table: "parent",
foreignKey: "parent_id",
hasMany: {childs: "child"},
columns: {table1Column: "text"}

});

Table2 = new JazzRecord.Model({
table: "child",
belongsTo: {parent: "parent"},
columns: {
table2Column: "text",
parent_id: "number"
}

});

//JazzRecord's internal picture of the schema must be updated/tables
created by calling migrate()
JazzRecord.migrate();

//Create parent record
//base record must be saved before associated records can be pushed
onto the association array
//create() is like newRecord() and save() combined
var record = Table1.create({
table1Column: "someText"
});

//Create child #1
//previously missing parenthesis
record.childs.push(Table2.newRecord({
table2Column: "someText"
}));

//Create child #2
//previously missing parenthesis
record.childs.push(Table2.newRecord({
table2Column: "anotherText"
}));

//Save all togegher to dataBase
record.save();

Sergio Viudes

unread,
Feb 8, 2010, 1:49:41 AM2/8/10
to jazzr...@googlegroups.com
Thanks for your answer!!
Sorry about missing parentheses, I wrote this code to ask you, it isn't my real code.
It's working 100%, thank you!


2010/2/8 Nick Carter <thyn...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "JazzRecord" group.
To post to this group, send email to jazzr...@googlegroups.com.
To unsubscribe from this group, send email to jazzrecord+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jazzrecord?hl=en.


Nick Carter

unread,
Feb 8, 2010, 2:07:59 AM2/8/10
to JazzRecord
Great to hear. Thanks for the interest in JazzRecord!

On Feb 8, 1:49 am, Sergio Viudes <djpep...@gmail.com> wrote:
> Thanks for your answer!!
> Sorry about missing parentheses, I wrote this code to ask you, it isn't my
> real code.
> It's working 100%, thank you!
>

> 2010/2/8 Nick Carter <thynct...@gmail.com>

> > jazzrecord+...@googlegroups.com<jazzrecord%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages