Skip to content
This repository was archived by the owner on Mar 9, 2020. It is now read-only.

Commit 5738ccc

Browse files
committed
Add Projects with a HABTM relation to People
1 parent 9274b40 commit 5738ccc

6 files changed

Lines changed: 48 additions & 1 deletion

File tree

app/models/person.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Person < ActiveRecord::Base
22
authenticates_with_sorcery!
33

4+
has_and_belongs_to_many :projects
5+
46
validates_confirmation_of :password
57
validates_presence_of :password, :on => :create
68
validates_presence_of :email

app/models/project.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Project < ActiveRecord::Base
2+
has_and_belongs_to_many :people
3+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateProjects < ActiveRecord::Migration
2+
def change
3+
create_table :projects do |t|
4+
t.string :name
5+
6+
t.timestamps
7+
end
8+
9+
create_join_table :people, :projects, id: false do |t|
10+
t.index :person_id
11+
t.index :project_id
12+
end
13+
end
14+
end

db/schema.rb

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

14-
ActiveRecord::Schema.define(version: 20140726092933) do
14+
ActiveRecord::Schema.define(version: 20140727135545) do
1515

1616
create_table "people", force: true do |t|
1717
t.string "login", null: false
@@ -45,4 +45,18 @@
4545

4646
add_index "people", ["email"], name: "index_people_on_email", unique: true
4747

48+
create_table "people_projects", id: false, force: true do |t|
49+
t.integer "person_id", null: false
50+
t.integer "project_id", null: false
51+
end
52+
53+
add_index "people_projects", ["person_id"], name: "index_people_projects_on_person_id"
54+
add_index "people_projects", ["project_id"], name: "index_people_projects_on_project_id"
55+
56+
create_table "projects", force: true do |t|
57+
t.string "name"
58+
t.datetime "created_at"
59+
t.datetime "updated_at"
60+
end
61+
4862
end

test/fixtures/projects.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
name: MyString
5+
6+
two:
7+
name: MyString

test/models/project_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'test_helper'
2+
3+
class ProjectTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)