0

I have 2 tables, one called movies and one called users. User can add a movie to their watchlist. When a user does so the app creates a new record in my movies table with the movie name, id, release date etc. So it's possible to have 3 records of the same movie. But all with different user id's. This is not preferable.

So I'm trying to figure out how to check if a record (movie with a movie_id value) already exists. If the movie already exists, to add the current users id to the user id colum so that multiple users can share the same movie record. If it does not exist yet, create a new record.

This is my current create function in my movies.rb controller,

def create
  respond_with Movie.create(movie_params.merge(user_id: current_user.id))
end

And in my movies model I have,

belongs_to :user

And I'm using Angular as my front-end framework, so this is my angular addMovie function,

  movieAdd.add()
    .then(function(response){
      $scope.movieListID = response;
      console.log ('Not empty' + $scope.movieListID)

      for (var i = 0; i < $scope.movieListID.releases.countries.length; i++) {
        var release = $scope.movieListID.releases.countries[i];
        if (release['iso_3166_1'] == 'NL') {
            releaseNL = release;
        }
      }

      if(typeof releaseNL === 'undefined'){
        // With release date

        Notification($scope.movieListID.original_title + ' is toegevoegd, maar heeft nog geen Nederlandse premiere datum.');

        createMovie.create({
          title:          $scope.movieListID.original_title,
          release_date:   $scope.movieListID.release_date,
          image:          $scope.movieListID.poster_path,
          movie_id:       $scope.movieListID.id
        }).then(init);

      } else {
        Notification.success($scope.movieListID.original_title + ' is toegevoegd.');

        createMovie.create({
          title:          $scope.movieListID.original_title,
          release_date:   releaseNL.release_date,
          image:          $scope.movieListID.poster_path,
          movie_id:       $scope.movieListID.id
        }).then(init);
      };

    })
Peter Boomsma
  • 8,851
  • 16
  • 93
  • 185
  • 1
    Your schema seems wrong. What are you trying to model? A user watching a movie or something? If so you will need more models eg MovieUser which allows a "habtm" relationship between movies and users. – Max Williams Nov 11 '15 at 14:33
  • I was thinking I could insert the current users id into the movie user id collum. That collum could have multiple user ids. – Peter Boomsma Nov 11 '15 at 14:50
  • This sounds ugly as hell, use a different table to model the relationship. That's how rails works. – Max Williams Nov 11 '15 at 14:57

1 Answers1

1

You can find info about has-and-belongs-to-many and has-many through relationships here. I would use one of those two methods to setup the relationship between users and movies. Then, I think, in the movie function you would:

  1. Check if the movie already exists in the database
  2. Build the relation and respond with the existing movie if it does exist.
  3. Create and respond with a new movie if it does not.
Community
  • 1
  • 1