samedi 1 octobre 2016

When to create an association/variable within a Rails model

I have a model called Messages, and it contains the usual associations / variables that make sense for a message, such as "sender_id", "recipient_id", "body", "subject", etc.

I am looking to create a set of 'quick message buttons' for users, that will be different for each user (and users can opt-out of this feature), and I was thinking of adding a new array variable called "quick_message_buttons" within the Message model, with a migration looking something like this:

class AddQuickMessageButtons < ActiveRecord::Migration
  def change
    add_column :messages, :quick_message_buttons, :string, array: true, default: []
  end
end

Before I go ahead, I had a hunch that this doesn't make much sense, and wanted to make sure if this is an ok thing to do. The reason I wasn't sure is, adding this variable would mean that every message model that gets created has this array even if users opt-out of the quick message buttons (ergo high coupling?).

Another idea I had was to create a whole new model called "QuickMessageButton" and associate them to each member. What is your opinion on the 'best practice' in situations like these?

On a side note, are there some good resources on the 'best practices' for MVC design? I've looked at several resources I can come across on Google, but perhaps veterans like you have found a better resource!

Thanks a lot!




Aucun commentaire:

Enregistrer un commentaire