NullVoxPopuli
NullVoxPopuli

Reputation: 65173

How do I migrate one class to be a subclass of another?

I'm using Ruby 1.8.7 and Rails 2.3.8

I have two ActiveRecord Models, A and B, each has their own tables and controllers.

B has many of the same attributes and methods as A
B is used for templating the A objects.
A has many more attributes as it is the real object, which is made from B.

So maybe B should be the super class to A? (Since A has all the attributes that B does, but not the other way around)

I already have the controller for B as a subclass to the controller for A.

How do I make one a subclass of the other, and still keep the separate tables?

Keeping the two controllers isn't that important, as the controller for B is like.. 10 lines.

And how would I call a method that might be part of an object? there is .try(), but I think it only works for attributes?

If I need to be more clear somehow, let me know.

Upvotes: 2

Views: 129

Answers (1)

tjarratt
tjarratt

Reputation: 1690

Have you consider using a mixin? They provide a lot of the same benefits as inheritance, but without having one class actually inherit from the other. Instead, you can provide class and instance methods and variables that are available to each class that includes the mixin.

I use this for a lot of models that all need to share an interface, or when I plan on using multiple models in some other part of the system that isn't pure MVC crud.

Upvotes: 1

Related Questions