开发者

Rails model structures

开发者 https://www.devze.com 2023-04-11 00:32 出处:网络
I saw some threads on this already on Stack, but wanted a little more clarification. I have seen many apps where there is a product model and category category model.开发者_开发技巧This is a has and

I saw some threads on this already on Stack, but wanted a little more clarification.

I have seen many apps where there is a product model and category category model. 开发者_开发技巧This is a has and belongs to many association, or a has_many through association.

I have also seen many apps where there is a user model and an email_address model. Email_address belongs to user, but user can have many email addresses.

My question is, would there ever be a situation where you can lump all the email addresses or categories into the user and product models, respectively? So in your user model, you will have email_one, email_two, etc?

What are the pros and cons of breaking it into different models? Thanks.


If the attribute is simple, it's almost certainly best to keep it in a single model - you can even serialize the attribute so that it takes, for example, and array of email_addresses. BUT (big but) you may well want to add a lot more information to an email address - which one is the primary one, when was it last profiled, email last sent to .. etc etc. This of course is much easier to handle if you have a separate email address model. So perhaps the question is really 'when should i use serialized attributes?'. My own answer would be 'only if I'm sure that I am storing something in that field that I never want to add further attributes to'. Usually that means it is something pretty peripheral to the main application, and about which no-one cares very much ...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号