Skip to content

Commit

Permalink
find singular or plural names
Browse files Browse the repository at this point in the history
  • Loading branch information
joeferner committed Jul 6, 2012
1 parent 1bf9e51 commit e870f91
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/model.js
Expand Up @@ -318,7 +318,7 @@ exports.define = function (name, columnDefs) {

Model.hasOne = function (associatedModel, opts) {
opts = this.normalizeHasOneOptions(associatedModel, opts);
var name = opts.name || inflection.camelize(associatedModel.modelName, true);
var name = opts.name || inflection.camelize(inflection.singularize(associatedModel.modelName), true);
if (this.associations[name]) {
return this;
}
Expand Down
9 changes: 7 additions & 2 deletions lib/query.js
Expand Up @@ -4,6 +4,7 @@ var persistUtil = require('./persist_utils');
var Connection = require('./connection');
var SqlTree = require('./sqltree');
var type = require('./type');
var inflection = require('./inflection');

var Query = function (connection, model) {
var self = this;
Expand Down Expand Up @@ -62,7 +63,9 @@ Query.prototype.include = function () {
var join;
if (typeof (arguments[0]) === 'string') {
var associationPropertyName = arguments[0];
var association = this.model.associations[associationPropertyName];
var association = this.model.associations[associationPropertyName]
|| this.model.associations[inflection.singularize(associationPropertyName)]
|| this.model.associations[inflection.pluralize(associationPropertyName)];
if (!association) {
throw new Error('Could not find association "' + associationPropertyName + '" off of "' + this.model.modelName + '"');
}
Expand Down Expand Up @@ -118,7 +121,9 @@ Query.prototype.leftJoin = function (otherTable, otherTableId, thisTableId) {
// type, associationPropertyName
Query.prototype._join = function (type, otherTable, otherTableId, thisTableId) {
if (arguments.length === 2) {
var association = this.model.associations[arguments[1]];
var association = this.model.associations[arguments[1]]
|| this.model.associations[inflection.singularize(arguments[1])]
|| this.model.associations[inflection.pluralize(arguments[1])];
otherTable = association.model.tableName;
if (association.type === 'hasOne') {
otherTableId = this.model.getIdColumn().dbColumnName;
Expand Down

0 comments on commit e870f91

Please sign in to comment.