# Find by different property

**URL:** <https://discuss.emberjs.com/t/find-by-different-property/2479>\
**Category:** Ember Data\
**Created:** [September 6, 2013, 1:34pm UTC](https://discuss.emberjs.com/t/find-by-different-property/2479 "2013-09-06T13:34:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davert](https://avatars.discourse-cdn.com/v4/letter/d/4bbf92/32.png) [@davert](https://discuss.emberjs.com/u/davert)\
**Post date:** [September 6, 2013, 1:34pm UTC](https://discuss.emberjs.com/t/find-by-different-property/2479/1 "2013-09-06T13:34:47Z")

</div>

Is there a way to find a record not by id. but via slug property. Still I want to keep ids in store as primary key.

if my route is “/post/:slug” then I want to fetch record by slug from API. Also I’d like to search the store if the record with the same slug already exists.

Can you advice something on implementation? I think that’s pretty common usage (with slugs), but unfortunately I got no ideas how to get this done right.

---

<div class="post-metadata">

**Author:** ![seif](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@seif](https://discuss.emberjs.com/u/seif)\
**Post date:** [September 6, 2013, 7:21pm UTC](https://discuss.emberjs.com/t/find-by-different-property/2479/2 "2013-09-06T19:21:25Z")

</div>

Yes. You simply need to add serilalize to your route, rename your path to the slug, then read it from model in the route

In your router

```
@route 'post', path: '/post/:slug'

```

In your routes

```
App.PostRoute = Ember.Route.extend(
  model: (params) ->
    App.Post.find params.slug

  setupController: (controller, model) ->
    controller.set 'content', model

  serialize: (model) ->
    slug: model.get 'slug'
)

```

---

<div class="post-metadata">

**Author:** ![lookingsideways](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lookingsideways/32/13652_2.png) [@lookingsideways](https://discuss.emberjs.com/u/lookingsideways)\
**Post date:** [September 7, 2013, 11:56am UTC](https://discuss.emberjs.com/t/find-by-different-property/2479/3 "2013-09-07T11:56:00Z")

</div>

(this refers to ember-data \<= 0.14, it may have changed with 1.0)

It seems as though the issue where doing this results in duplicate records still hasn’t been resolved.

Unless your backend always returns the slug as a reference then you could end up with one record with the numeric ID and one with the slug ID.

`App.Post.find(params.slug)` will add a model with the slug as the ID to the store (even if the numeric ID is returned from the server)

`App.Post.find(id)` will add a model with the numeric ID to the store

There are workarounds for it. I’m using the following in my user route:

```auto
App.Router.map ->
  @resource 'user', { path: '/user/:user_id' }

App.UserRoute = Ember.Route.extend
  serialize: (model, params) ->
    if model && model.get
      { user_id: model.get('username') }

  model: (params) ->
    users = App.User.find({id: params.user_id})
    users.one 'didLoad', ->
      users.resolve(users.get('firstObject'))
    return users

```

Note that this will perform a query to `/users` not `/users/:id`, hence the need to grab the first result.

If anyone has a more elegant solution to this I’d be glad to hear it!

---

<div class="post-metadata">

**Author:** ![davert](https://avatars.discourse-cdn.com/v4/letter/d/4bbf92/32.png) [@davert](https://discuss.emberjs.com/u/davert)\
**Post date:** [September 7, 2013, 7:50pm UTC](https://discuss.emberjs.com/t/find-by-different-property/2479/4 "2013-09-07T19:50:32Z")

</div>

@lookingsideways Thanks, I had similar thoughts with fetching item by query and resolving it into one record. Unfortunately something didn’t work for me (maybe I misuse promises of 1.0beta). Your solution can be improved if a map for “slug” =\> “id” is created, so models can be cached by their slug.

Anyway, I decided to follow KISS and set the slug as the primary key for my records. That simplified code. In my project I have opportunity to get non-unique slugs, depending on a project. Thus, I will clean up store on switching a project.
