module.exports = bookshelf.Model.extend({
  tableName: 'community',

  memberships: function() {
    return this.hasMany(Membership);
  },

  users: function() {
    return this.belongsToMany(User, 'users_community', 'community_id', 'users_id');
  },

  invitations: function() {
    return this.hasMany(Invitation);
  },

  posts: function() {
    return this.belongsToMany(Post, 'post_community', 'community_id', 'post_id');
  },

  moderators: function() {
    return this
      .belongsToMany(User, 'users_community', 'community_id', 'users_id')
      .query({where: {role: Membership.MODERATOR_ROLE}});
  }

}, {

  find: function(id_or_slug) {
    if (isNaN(Number(id_or_slug))) {
      return Community.where({slug: id_or_slug}).fetch();
    }
    return Community.where({id: id_or_slug}).fetch();
  },

  members: function(communityId, options) {
    _.defaults(options, {
      limit: 10,
      offset: 0
    });

    return Community.find(communityId).then(function(community) {
      return community.users().query(function(qb) {
        if (options.term) {
          qb.where("name", "ILIKE", '%' + options.term + '%');
        }
        qb.where("users.active", "=", true);

        qb.limit(options.limit);
        qb.offset(options.offset);
      }).fetch();
    })
  }

});

@@ -1054,20 +1054,22 @@
 options.
-term
+search
 ) %7B%0A    
@@ -1118,12 +1118,14 @@
 ons.
-term
+search
  + '

fix searching for members