Skip to content

Add a has_many through association to the dummy app #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/dummy/app/models/membership.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Membership < ApplicationRecord
belongs_to :user
belongs_to :organization
end
6 changes: 6 additions & 0 deletions test/dummy/app/models/organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Organization < ApplicationRecord
has_many :memberships
has_many :users, through: :memberships
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class User < ApplicationRecord
before_create :foo, -> () {}
validates :name, presence: true
validates :first_name, presence: true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(fixing invalid field)

has_one :profile
scope :adult, -> { where(age: 18..) }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateOrganizations < ActiveRecord::Migration[7.1]
def change
create_table :organizations do |t|
t.string :name

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions test/dummy/db/migrate/20240503153021_create_memberships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateMemberships < ActiveRecord::Migration[7.1]
def change
create_table :memberships do |t|
t.references :user, null: false, foreign_key: true
t.references :organization, null: false, foreign_key: true

t.timestamps
end
end
end
19 changes: 18 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

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

create_table "memberships", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "organization_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["organization_id"], name: "index_memberships_on_organization_id"
t.index ["user_id"], name: "index_memberships_on_user_id"
end

create_table "organizations", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "posts", force: :cascade do |t|
t.string "title"
t.text "body"
Expand All @@ -34,4 +49,6 @@
t.datetime "updated_at", null: false
end

add_foreign_key "memberships", "organizations"
add_foreign_key "memberships", "users"
end