开发者

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 a开发者_开发知识库nd 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?


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.


Extend class:

class Class2 < Class1
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜