Skip to content
This repository was archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #42 from janmeier/findorcreateDocs
Browse files Browse the repository at this point in the history
docs for findorcreateAdditional attribute
  • Loading branch information
sdepold committed May 26, 2013
2 parents 4b6db59 + ea42fe6 commit ddfa799
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions views/documentation/sections/models/data_retrieval.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ The method `findOrCreate` can be used to check if a certain element is already e
Let's assume we have an empty database with a `User` model which has a `username` and a `job`.

```js
User.findOrCreate({ username: 'sdepold' }, { job: 'Technical Lead JavaScript' }).success(function(user) {
User.findOrCreate({ username: 'sdepold' }, { job: 'Technical Lead JavaScript' }).success(function(user, created) {
console.log(user.values)
console.log(created)

/*
{
Expand All @@ -44,18 +45,20 @@ User.findOrCreate({ username: 'sdepold' }, { job: 'Technical Lead JavaScript' })
createdAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET),
updatedAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET)
}
created: true
*/
})
```

The code created a just created instance.
The code created a new instance.

So when we already have an instance ...

```js
User.create({ username: 'fnord', job: 'omnomnom' }).success(function() {
User.findOrCreate({ username: 'fnord' }, { job: 'something else' }).success(function(user) {
User.findOrCreate({ username: 'fnord' }, { job: 'something else' }).success(function(user, created) {
console.log(user.values)
console.log(created)

/*
{
Expand All @@ -65,12 +68,13 @@ User.create({ username: 'fnord', job: 'omnomnom' }).success(function() {
createdAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET),
updatedAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET)
}
created: false
*/
})
})
```

... the existing entry will not be changed. See the `job` of the second user.
... the existing entry will not be changed. See the `job` of the second user, and the fact that created was false.

##### findAll - Search for multiple elements in the database | findAll

Expand Down

0 comments on commit ddfa799

Please sign in to comment.