1
0
Fork 0

Add model Followship

This commit is contained in:
Alex Kotov 2021-03-10 15:34:59 +05:00
parent 23cf15a705
commit 15680d6172
5 changed files with 53 additions and 1 deletions

9
app/models/followship.rb Normal file
View File

@ -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

View File

@ -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

14
db/schema.rb generated
View File

@ -10,7 +10,17 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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| create_table "posts", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
@ -29,5 +39,7 @@ ActiveRecord::Schema.define(version: 2021_03_10_092955) do
t.string "description" t.string "description"
end end
add_foreign_key "followships", "profiles", column: "object_profile_id"
add_foreign_key "followships", "profiles", column: "subject_profile_id"
add_foreign_key "posts", "profiles" add_foreign_key "posts", "profiles"
end end

9
test/fixtures/followships.yml vendored Normal file
View File

@ -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

View File

@ -0,0 +1,7 @@
require "test_helper"
class FollowshipTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end