Add model Followship
This commit is contained in:
parent
23cf15a705
commit
15680d6172
5 changed files with 53 additions and 1 deletions
9
app/models/followship.rb
Normal file
9
app/models/followship.rb
Normal 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
|
15
db/migrate/20210310102531_create_followships.rb
Normal file
15
db/migrate/20210310102531_create_followships.rb
Normal 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
14
db/schema.rb
generated
|
@ -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
|
||||
|
|
9
test/fixtures/followships.yml
vendored
Normal file
9
test/fixtures/followships.yml
vendored
Normal 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
|
7
test/models/followship_test.rb
Normal file
7
test/models/followship_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class FollowshipTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Reference in a new issue