Pregunta

I'm still pretty inexperienced with Rails and would like a hand planning the models and associations in an app that I'd like to build. It's pretty basic, but there's some functionality that I'm having trouble figuring out. It will essentially be a scaled down rip-off of MyAnimeList.

Here's the deal:

I currently have a User model and a Anime model. Anime contains a list of anime shows. I also have a List model which has one user and presumably will have many anime, depending on you guys' suggestions.

The first feature seems fairly straightforward, but I trip up on the two additional ones that I'd like to include:

  1. I'd like users to be able to go to the main Anime list and add anime to their own list by clicking an 'add to my list' button.
  2. Users can score their anime, and change how many episodes/shows they've completed. Stats for this should be reflected back on the master list, for example if someone finished watching the Sword Art Online (SAO) anime and then scored it and marked it as finished, it would update the SAO listing on the main anime list, showing stats like '2987 people have finished this anime' and 'the average user rating for this show is 4.2 out of 5'. Perhaps a Stat model? How could I implement this?
  3. Occasionally, a show might accidentally be duplicated in the database, or have multiple translations from Japanese. I'll need the ability to merge shows (and all of their associated stats/users etc).

So I'm just not sure what models to include (is a Stat model necessary?), or what needs to be nested where (ie nested resource), if at all.

I've looked at many different guides/tutorials/similar questions etc but haven't had any luck taking what I've learned and applying it to (for me) complex scenarios.

Any help or discussion on this would be greatly appreciated! Just let me know if you need any further details.

¿Fue útil?

Solución

First off, never ever call your model 'Show'. It's confusing, ambiguous and simply doesn't make clear what's the class all about. If you would tell me you have a Show model, how would I know what you're referring to? Naming is really important, so it's worth investing time in.

Anyway, I would start with drawing a basic setup. It would definitely help to make your setup visual.

Your explanation is a bit confusing, but you could think of the current setup:

  1. A User has_one lists, therefor WatchList belongs_to a User
  2. A WatchList has_many animes, therefor Anime belongs_to a WatchList
  3. A Anime has_many review_scores, therefor ReviewScore belongs_to an Anime

This is just an example, and only shortly thought trough. Hopefully it gives you a basic idea of how you could think about your main building blocks.

Update: MrMorphe is right that List is also a bad name for a model. Pretty much for the same reasons as Show. To be honest, Score is also pretty bad, you should really consider all names carefully.

Otros consejos

My thoughts: Try not to think in terms of lists. List is a generic data type, rather try to think in the more abstract terms of the concept/s that you are modelling (e.g. WatchedShows). These "abstract concepts" often become your models.

As Peter suggests, Anime is better than Show. Might there be more you want to model about an anime series though e.g. episodes, seasons, etc? Thinking about this now will save you issues when you come to look at merging things...

Now I'm a novice with Rails myself and rusty on my data-modelling so please someone jump on me if I'm wrong with the following:

You have a relationship between the User and an Anime. The relationship is a model (WatchedAnime?) where one User has watched(?) many Animes, and an Anime has been watched(?) by many Users. To model this you might want to look at the has_many :through association at http://guides.rubyonrails.org/association_basics.html. The score the user assigns would then be an element of this relationship.

You could then work out an animes score by a simple statement e.g.

average_score = WatchedAnime.where("anime_id = 100").avg('score')

EDITS: You will also want to ignore instances where the user hasn't assigned a score or this will throw the average off.

As said, I'm a novice with Rails and the problem with the has_many :through relationship is that you with have to validate that the user does not have an existing relationship with a given anime, hence I wonder if rails has a more natural way of modelling/refining this association...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top