← Back to Upcase

Storing data in a has_many :through model


(Zack Fernandes) #1

Hi there, I was wondering if it’s feasible to store data in a model which is used for a has_many :through association.

For example, I’ve got a User and an Advisor. I could, of course, add advisor_id to the User and use a regular has_many association in Advisor; however I’d also like to store metadata about this relationship such as what permissions the Advisor has over the User’s account.

I’m currently debating two ways of accomplishing this:

  1. Vanilla has_many - Simply add an advisor_id and permissions column to User and has_many :users to Advisor.
  2. has_many :through + serialized data - Create another model to store the user_id, advisor_id, and whatever metadata we decide to keep about this relationship. This would allow me to keep everything related to advisor/client relationships, which won’t apply to all users, contained within it’s own world.

I’d love some feedback on which approach is “right,” as I’m torn between the two.

Thanks!


(Lenart Rudel) #2

I think you should go with the 2nd approach - use association model. In the case when you need to store relation metadata this is the way to go.