Specifying pluralization of models


#1

I’m converting a toy Rails app (Twitter clone) to use Ember and I’ve run across an interesting problem. My primary model is App.Status, but when it makes the ajax call to retrieve the statuses, it calls out to /statuss.

I didn’t find any official documentation on specifying pluralization of model names, but after some googling found that passing plurals: { model: 'models' } to DS.Store.extend() allows it. However, I didn’t find that this was the case. I’m not sure if this is a bug because I don’t know of the canonical way to do this. I’d be happy to send a PR to update docs if there’s a canonical way of doing this — and to update the code if this is indeed a bug.

My store.js:

Thing.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.RESTAdapter.create({
    plurals: {
      status: 'statuses'
    }
  })
});

Console output because forum noobs can’t post screenshots:

> store = Ember.get(DS, 'defaultStore')
  Class { … }
> store.adapter.plurals
  Object {status: "statuses"}
> store.adapter.pluralize('status')
  "statuss"

#2

Reproducing the problem: http://jsbin.com/itopaw/3/edit


#3

Doesn’t the plurals object need to be inside of the configurations object?


#4

In serializer.js…

  // define a plurals hash in your subclass to define
  // special-case pluralization
  pluralize: function(name) {
    var plurals = this.configurations.get('plurals');
    return (plurals && plurals[name]) || name + "s";
  },

#5

Something like this? http://jsbin.com/itopaw/6/edit

It extends DS.RESTAdapter and my App.Store uses that instead of using DS.RESTAdapter directly. This works, but I thought there was supposed to be a way to tell Ember what the plurals were rather than tell it how to find them.


#6

Check out http://emberjs.com/guides/models/the-rest-adapter/

DS.RESTAdapter.configure("plurals", {
  person: "people"
});

#7

Huh, it’s right there, “pluralization customization”. It’s even in bold caps. I was looking for it in the store docs, since I figured it’d be a higher-level concern than being specific to the adapter — for serializing into JSON, for example. Thank you for pointing it out. :slight_smile:


#8

Glad to help out a fellow ember-data brother :slight_smile:


#9

Hi All,

I was working on the railscasts example of implementing ember and rails together (new to ember) and I’m having trouble with that pluralize bit.

In my store.js.coffee I have this: Raffler.Store = DS.Store.extend revision: 4 adapter: DS.RESTAdapter.create()

DS.RESTAdapter.configure(“plurals”, entry: “entries”)

However when loading the app it still looks for /entrys.

Any thoughts for other things I can check? Apologies if this is a newbie question.

Thanks!


#10

It should be:

App.restSerializer.configure("plurals", {
  address: "addresses"
});

#11

We are working on improving the docs for Ember Data so this shouldn’t be so confusing in the future.


#12

I am still in the ‘lets get an inflector’ camp. This basically costs us 1kb gzip’d to get a rails compatible inflector. Maybe this is just optional for those who use ember-data with a rails/ams backend. But it does help satisfy the “it-just-works” case.

Old implementation, but we can use it to start a discussion: