What would be the best method to store this data?

Hi, I've been learning rails and have this question for a project I'm working on:

I have a User model and models which are mostly there to be databases of things to be rated by the user. I'll eventually make it possible (probably a 3rd model) to compare users likes and dislikes.

The user will look through the lists from the models and choose to love, like, ignore, dislike or hate each item.

For example if I had an 'Animals' category each row of that table contains the name, and possibly a few fields such as 'times liked' and 'times loved'.

I need to store the loved/liked/disliked/hated object linked to the users. I've had a couple of possible ideas how to do this but am not sure which would work best..

I could create a column to store each categories info in a serialized hash of arrays - if a user was rating animals it could be saved in a column for animals with {loved: [cats, fish], liked: [dogs]} etc

Would I then be able to retrieve the info with User.animals[:loved]?

I could alternatively create multiple associations between the models if that is possible? User has_many loved_animals, class_name: Animals, User has_many liked_animals, class_name: Animals

User.loved_animals << Cat, Fish User.liked_animals << Dog Would either of these work? is there a better solution? What would be the pros and cons of them? Thanks,

1 thought on “What would be the best method to store this data?”

Leave a Comment