diff --git a/app/models/followship.rb b/app/models/followship.rb new file mode 100644 index 0000000..ce9e485 --- /dev/null +++ b/app/models/followship.rb @@ -0,0 +1,9 @@ +class Followship < ApplicationRecord + belongs_to :subject_profile, class_name: 'Profile' + + belongs_to :object_profile, class_name: 'Profile' + + validates :object_profile, + exclusion: { in: ->(followship) { [followship.subject_profile] } }, + uniqueness: { scope: :subject_profile } +end diff --git a/db/migrate/20210310102531_create_followships.rb b/db/migrate/20210310102531_create_followships.rb new file mode 100644 index 0000000..588cc21 --- /dev/null +++ b/db/migrate/20210310102531_create_followships.rb @@ -0,0 +1,15 @@ +class CreateFollowships < ActiveRecord::Migration[6.1] + def change + create_table :followships do |t| + t.timestamps + t.references :subject_profile, + null: false, + foreign_key: { to_table: :profiles } + t.references :object_profile, + null: false, + foreign_key: { to_table: :profiles } + + t.index %i[subject_profile_id object_profile_id], unique: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 88a396b..2cf2692 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_03_10_092955) do +ActiveRecord::Schema.define(version: 2021_03_10_102531) do + + create_table "followships", force: :cascade do |t| + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.integer "subject_profile_id", null: false + t.integer "object_profile_id", null: false + t.index ["object_profile_id"], name: "index_followships_on_object_profile_id" + t.index ["subject_profile_id", "object_profile_id"], name: "index_followships_on_subject_profile_id_and_object_profile_id", unique: true + t.index ["subject_profile_id"], name: "index_followships_on_subject_profile_id" + end create_table "posts", force: :cascade do |t| t.datetime "created_at", precision: 6, null: false @@ -29,5 +39,7 @@ ActiveRecord::Schema.define(version: 2021_03_10_092955) do t.string "description" end + add_foreign_key "followships", "profiles", column: "object_profile_id" + add_foreign_key "followships", "profiles", column: "subject_profile_id" add_foreign_key "posts", "profiles" end diff --git a/test/fixtures/followships.yml b/test/fixtures/followships.yml new file mode 100644 index 0000000..f39ca6f --- /dev/null +++ b/test/fixtures/followships.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + subject_profile: one + object_profile: two + +two: + subject_profile: two + object_profile: one diff --git a/test/models/followship_test.rb b/test/models/followship_test.rb new file mode 100644 index 0000000..64d2080 --- /dev/null +++ b/test/models/followship_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class FollowshipTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end