Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a user's profile be a separate model?

I'm learning Rails by building a simple site where users can create articles and comment on those articles. I have a view which lists a user's most recent articles and comments. Now I'd like to add user 'profiles' where users can enter information like their location, age and a short biography. I'm wondering if this profile should be a separate model/resource (I already have quite a lot of fields in my user model because I'm using Authlogic and most of it's optional fields).

What are the pros and cons of using a separate resource?

like image 219
Yergin Avatar asked Nov 09 '09 22:11

Yergin


2 Answers

I'd recommend keeping profile columns in the User model for clarity and simplicity. If you find that you're only using certain fields, only select the columns you need using :select.

If you later find that you need a separate table for some reason (e.g. one user can have multiple profiles) it shouldn't be a lot of work to split them out.

I've made the mistake of having two tables and it didn't buy me anything but additional complexity.

like image 86
Marcus Avatar answered Sep 28 '22 14:09

Marcus


  • Pros: It simplifies each model
  • Cons: Managing 2 at once is slightly harder

It basically comes down to how big the user and profile are. If the user is 5 fields, and the profile 3, there is no point. But if the user is 12 fields, and the profile 20, then you definitely should.

like image 27
Macha Avatar answered Sep 28 '22 13:09

Macha