user929062
user929062

Reputation: 817

Rails architecture, better create a a new model or add a boolean value to existing model?

I have an article model article.rb (with related images):

attr_accessible :name, :content, :image_ids, :altname1

I now want to create another kind of articles, that are just for cities. They will appear in a completely different section of the website and are not related to all other articles. But the model is exactly the same.

Is it better to create a new model cityarticle.rb or is it better to add a new boolean column (cityarticle with true and false as options) into the existing article model. I'd then add a new action to the controller:

def cityarticles
  @cityarticles = Article.where("cityarticle = ?", true)
end

For me it's easier to keep just one model, but there might be good reasons for a new model? How about performance?

Upvotes: 0

Views: 95

Answers (2)

sockmonk
sockmonk

Reputation: 4255

Some questions to ask yourself: In what other ways will these types of articles be different? Will different people have access to create/edit/delete them? How do I create a city article and not let it accidentally be saved as a non-city article, or vice versa? Am I using attr_accessible or strong_parameters to prevent users from overriding the article type with params passed in? Will the different types have different validation rules (maybe requiring a city name for city articles)? How hard will it be to get just the articles I want? How likely am I to forget to specify what kind of articles to show and show all of them?

Based on answers to questions like those, you should be able to decide what data structure will work best for you. If you use Rails' STI pattern, you could wind up with one table but two different models, as one solution. Performance is probably not the main consideration, but if the two types of articles are never going to be queried together, it might be slightly better in terms of performance to split them into separate tables.

Upvotes: 1

New model represents a new "entity" on the System.

Say CityArticles extends Article

It should be a new Model for code clarity and extensibility to increment functionality over the "CityArticles".

You can make a new table scaffold or migration:

rails g scaffold CityArticles references:article

Or making Article class polimorfic association. Read http://teachmetocode.com/articles/ruby-on-rails-what-are-polymorphic-associations/

Upvotes: 1

Related Questions