Backo
Backo

Reputation: 18871

Create a class that is the "same" as another class

I am using Ruby on Rails 3.0.10 and I would like to create a new class\model Class2 that has the same behavior (methods, constants, ...) and that is related to the same database table and ... of a class\model Class2. That is, the Class2 must be exactly the same as the Class1.

How can I do that?


Motivations

  1. I would like to do that because I have the Class1 (and a related database table) that is used to store some association data (:has_many and :belongs_to) and exactly the same associations can be handled also by the Class2 in a equal way as the Class1 makes.

  2. Also, I would like to do that because I would "organize" application files keeping the "Ruby on Rails Way" of make things. That is, I would like to create related view files in a separated folders for both the Class1 and the Class2 (maybe, it is just my "standardize things mania"!).

What about my motivations in order to create the new Class1?


What problems could I have in future developments?

Upvotes: 0

Views: 89

Answers (2)

AntonKrutikov
AntonKrutikov

Reputation: 53

Extend class:

class Class2 < Class1
end

Upvotes: 0

Nexerus
Nexerus

Reputation: 1088

You want to extend the class? this should do the trick:

class MoreUsers < Users
  set_table_name "users"
end

Hope this is the answer you're looking for.

Upvotes: 4

Related Questions