Skip to content

Commit edc5ef3

Browse files
authored
Add a has_many through association to the dummy app (#367)
1 parent d521325 commit edc5ef3

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

test/dummy/app/models/membership.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
class Membership < ApplicationRecord
4+
belongs_to :user
5+
belongs_to :organization
6+
end

test/dummy/app/models/organization.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
class Organization < ApplicationRecord
4+
has_many :memberships
5+
has_many :users, through: :memberships
6+
end

test/dummy/app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class User < ApplicationRecord
44
before_create :foo, -> () {}
5-
validates :name, presence: true
5+
validates :first_name, presence: true
66
has_one :profile
77
scope :adult, -> { where(age: 18..) }
88

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreateOrganizations < ActiveRecord::Migration[7.1]
2+
def change
3+
create_table :organizations do |t|
4+
t.string :name
5+
6+
t.timestamps
7+
end
8+
end
9+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateMemberships < ActiveRecord::Migration[7.1]
2+
def change
3+
create_table :memberships do |t|
4+
t.references :user, null: false, foreign_key: true
5+
t.references :organization, null: false, foreign_key: true
6+
7+
t.timestamps
8+
end
9+
end
10+
end

test/dummy/db/schema.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.1].define(version: 2024_04_03_145625) do
13+
ActiveRecord::Schema[7.1].define(version: 2024_05_03_153021) do
1414
create_table "composite_primary_keys", primary_key: ["order_id", "product_id"], force: :cascade do |t|
1515
t.integer "order_id"
1616
t.integer "product_id"
@@ -19,6 +19,21 @@
1919
t.datetime "updated_at", null: false
2020
end
2121

22+
create_table "memberships", force: :cascade do |t|
23+
t.integer "user_id", null: false
24+
t.integer "organization_id", null: false
25+
t.datetime "created_at", null: false
26+
t.datetime "updated_at", null: false
27+
t.index ["organization_id"], name: "index_memberships_on_organization_id"
28+
t.index ["user_id"], name: "index_memberships_on_user_id"
29+
end
30+
31+
create_table "organizations", force: :cascade do |t|
32+
t.string "name"
33+
t.datetime "created_at", null: false
34+
t.datetime "updated_at", null: false
35+
end
36+
2237
create_table "posts", force: :cascade do |t|
2338
t.string "title"
2439
t.text "body"
@@ -34,4 +49,6 @@
3449
t.datetime "updated_at", null: false
3550
end
3651

52+
add_foreign_key "memberships", "organizations"
53+
add_foreign_key "memberships", "users"
3754
end

0 commit comments

Comments
 (0)