Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STI Equivalent #6

Open
tapajos opened this issue Jul 2, 2010 · 1 comment
Open

STI Equivalent #6

tapajos opened this issue Jul 2, 2010 · 1 comment

Comments

@tapajos
Copy link
Member

tapajos commented Jul 2, 2010

Issue from: http://github.com/couchrest/couchrest/issues#issue/18

Hello,

Has anyone already thought of starting to work on something similar to active record's single table inheritance ?

Let's guess two classes :

class User < CouchRest::ExtendedDocument
end

class Admin < User
end

Currently, couchrest wouldn't see that the admin is in fact a user. We couldn't retrieve all users (admins included).

The idea would be, when a class inherits from an other, to add an attribute "couchrest-sti", which would have the subclass value.
When we do a User.all, it's retrieve all the objects having "couchrest-type" defined to user.
But if it has a "couchrest-sti" defined, it'd instantiate the object as the class defined in that attribute. Not as a user.

I really need this. So I'd be willing to work on it. But is it something you'd find useful and that'd have a chance to get implemented to the trunk ?

@samlown
Copy link
Member

samlown commented Jul 2, 2010

Defining an 'sti' column is not such a good idea as it would only help with one level of inheritance. For anyone wanting to implement this, I'd suggest defining your own view which accepts the multiple types you're searching for, for example:

class User < CouchRest::ExtendedDocument
  view_by :all_types,
    :map => "
      function(doc) {
        switch (doc['couchrest-type']) {
          case 'User':
          case 'Admin':
            emit(doc['_id'], 1);
          default:
        }
      }
    "
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants