settinghead
settinghead

Reputation: 95

Similar models in ruby on rails

I'm working on a Ruby on Rails project where I have a "post" model and a "reply" model. As their names suggest, they share a lot of common properties such as "author_id" and "body", but have their own respective unique properties as well (e.g. a post has a title, whereas a reply does not).

My question is: what's the best practice for dealing with two similar data models? Right now I'm treating them as two distinct models types, but as I start coding, I realized there will be a lot of repetition of code.

Upvotes: 1

Views: 453

Answers (2)

Dipil
Dipil

Reputation: 2708

I think the best option here would be for you to create a module with the shared methods for each one and include it in both of them. That way, you don't have to repeat code and both will be independent.

You can check out the link provided by sczizzo above.

Upvotes: 1

sczizzo
sczizzo

Reputation: 3206

Take a look at mixins and modules in Ruby.

Upvotes: 1

Related Questions