From 68e74a9d85d55a75e476d04756969a8a0e20a667 Mon Sep 17 00:00:00 2001 From: "leo.selig" Date: Thu, 4 Feb 2016 12:38:46 +0100 Subject: [PATCH] Add association User has many CodeHarborLink - add migration tht associates User with CodeHarborLink - add belongs_to from CodeHarborLink to User - changed CodeHarborLinkController#index to only serve current user's CodeHarborLinks (leoselig/codeocean#2) --- app/controllers/code_harbor_links_controller.rb | 3 ++- app/models/code_harbor_link.rb | 3 +++ db/migrate/20160204111716_add_user_to_code_harbor_link.rb | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20160204111716_add_user_to_code_harbor_link.rb diff --git a/app/controllers/code_harbor_links_controller.rb b/app/controllers/code_harbor_links_controller.rb index 3b71a050..4763976b 100644 --- a/app/controllers/code_harbor_links_controller.rb +++ b/app/controllers/code_harbor_links_controller.rb @@ -10,7 +10,7 @@ class CodeHarborLinksController < ApplicationController # GET /code_harbor_links # GET /code_harbor_links.json def index - @code_harbor_links = CodeHarborLink.paginate(page: params[:page]) + @code_harbor_links = CodeHarborLink.where(user_id: current_user.id).paginate(page: params[:page]) authorize! end @@ -56,6 +56,7 @@ class CodeHarborLinksController < ApplicationController # Use callbacks to share common setup or constraints between actions. def set_code_harbor_link @code_harbor_link = CodeHarborLink.find(params[:id]) + @code_harbor_link.user = current_user authorize! end diff --git a/app/models/code_harbor_link.rb b/app/models/code_harbor_link.rb index ea91ab71..54250644 100644 --- a/app/models/code_harbor_link.rb +++ b/app/models/code_harbor_link.rb @@ -1,5 +1,8 @@ class CodeHarborLink < ActiveRecord::Base validates :oauth2token, presence: true + validates :user_id, presence: true + + belongs_to :user def to_s oauth2token diff --git a/db/migrate/20160204111716_add_user_to_code_harbor_link.rb b/db/migrate/20160204111716_add_user_to_code_harbor_link.rb new file mode 100644 index 00000000..8fa36ed1 --- /dev/null +++ b/db/migrate/20160204111716_add_user_to_code_harbor_link.rb @@ -0,0 +1,5 @@ +class AddUserToCodeHarborLink < ActiveRecord::Migration + def change + add_reference :code_harbor_links, :user, index: true, foreign_key: true + end +end