user1885058
user1885058

Reputation: 599

How should I better organize related data tables in a rails app?

I have a User model generated with Devise.

Now I want every user to have other columns

- balance
- paperclip logo
- website
- address
- phone
etc

What is the best way to organize such data?

Should I make separate tables (like Phones table, Address table, Userlogo table etc) and establish One-To-One relationship?

I've heard that putting everything to one table is bad.

Would be nice to hear some opinions.

Upvotes: 1

Views: 113

Answers (1)

Khaled
Khaled

Reputation: 2091

Putting everything to one table:

  • Benefits: No need for joins to get the user model, which saves query time
  • Drawbacks: User model will contain a a lot of data, so if you are holding an array of 1000 users, they will consume memory

Separating attributes:

  • Benefits: User model will consume little memory (only basics such as email, name, ....)
  • Drawbacks: Lots of joins to get info about the user

I think you choice depends a lot on what your application does, so if you always need all info about user, then put everything in one table other wise you could separate data. Anyway, you could do both so you could have the user model and another model named user_data, and that model will hold all the extra information such as phone, address, etc ... this way you get the benefits from both approaches.

Upvotes: 1

Related Questions