I have a rails app which currently has 3 models: Post, User and Movie. I've listed the important fields in the models below
Post: id: integer, user_id: integer, location_id: integer, description: string
Movie: id: integer, name: string, rating: decimal, ...
User: id: integer, email: string, encrypted_password: string ...
Now I want Users to be able to create Posts about a Movie, and my site will display these Posts. If there is a pre-existing Post about a Movie, then the rating that the User assigns to the Movie should be added and averaged with the pre-existing rating, and the description that the User assigns should be added to the list of descrip开发者_JAVA百科tions already present in the Movie.
First question: How do I store a list of descriptions persistently as part of my Movie model? For example, is it possible to store an array of strings as part of the Movie model, or should I create a new Description model just for this purpose?
And after this, how do I associate these models so they behave as expected? My thoughts were:
Post -> belongs_to -> User, User -> has_many -> Posts, Post -> has_one -> Movie but I'm not sure this is the right way to associate them, and I do not have any experience with :through associations (which might be required here?)
The correct associations would be:
User has_many Post
Movie has_many Post
Post belongs_to User
Post belongs_to Movie
You'll need to add a movie_id column to posts table. Descriptions should stay in the Post model, not in the Movie model. When you want to show the posts on a particular movie, @movie.posts should get you there.
加载中,请稍侯......
精彩评论