Red
Red

Reputation: 2246

Preventing Inheritance in MongoMapper

I am using Rails with MongoDB and MongoMapper. My problem is that I have a class that is inheriting from another and I want to leave out one of the keys. For example:

class A
    include MongoMapper::EmbeddedDocument
    many :items
    #Other keys I want
end

class Item < A
    include MongoMapper::EmbeddedDocument
    #Included Keys from A
    #Other Keys that I want
end

The problem here is that Item inherits the relationship from A of many :items. How can I prevent that?

Upvotes: 0

Views: 61

Answers (1)

mu is too short
mu is too short

Reputation: 434785

This:

a class that is inheriting from another and I want to leave out one of the keys

indicates that that you don't have a valid relationship for inheritance. Perhaps you want something more like this:

class B
    # Common things for A and C
end

class A < B
    many :items
    # Other things that shouldn't be in B or C
end

class C < B
    # Other keys you want that aren't already in B
end

Trying to narrowing the interface of a derived class is a sign that you're doing something wrong and need to rethink your hierarchy.

Upvotes: 2

Related Questions