Last active
October 25, 2015 12:31
-
-
Save ttanimichi/e87008682038541601be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/http/app/models/category.rb b/http/app/models/category.rb | |
index e1f8fac..98869aa 100644 | |
--- a/http/app/models/category.rb | |
+++ b/http/app/models/category.rb | |
@@ -1,5 +1,5 @@ | |
class Category < ActiveRecord::Base | |
- has_many :topics | |
+ has_many :topics, foreign_key: :category_code | |
validates :name, presence: true, length: { maximum: 255 } | |
validates :code, presence: true, length: { maximum: 255 } | |
diff --git a/http/app/models/client.rb b/http/app/models/client.rb | |
index ec00479..a799b99 100644 | |
--- a/http/app/models/client.rb | |
+++ b/http/app/models/client.rb | |
@@ -1,6 +1,6 @@ | |
class Client < ActiveRecord::Base | |
- has_many :posts | |
- has_many :topics | |
+ has_many :posts, foreign_key: :client_code | |
+ has_many :topics, foreign_key: :client_code | |
before_create -> { self.code = Saikoro.alphanumerics(length: 16) } | |
diff --git a/http/app/models/post.rb b/http/app/models/post.rb | |
index e365ce9..d8c97e0 100644 | |
--- a/http/app/models/post.rb | |
+++ b/http/app/models/post.rb | |
@@ -1,6 +1,6 @@ | |
class Post < ActiveRecord::Base | |
- belongs_to :client | |
- belongs_to :topic, counter_cache: true, touch: true | |
+ belongs_to :client, foreign_key: :client_code | |
+ belongs_to :topic, foreign_key: :topic_code, counter_cache: true, touch: true | |
def to_h | |
{ | |
diff --git a/http/app/models/topic.rb b/http/app/models/topic.rb | |
index 1a7f6af..5765d65 100644 | |
--- a/http/app/models/topic.rb | |
+++ b/http/app/models/topic.rb | |
@@ -1,10 +1,12 @@ | |
class Topic < ActiveRecord::Base | |
- has_many :posts | |
- belongs_to :client | |
- belongs_to :category | |
+ has_many :posts, foreign_key: :topic_code | |
+ | |
+ belongs_to :client, foreign_key: :client_code | |
+ belongs_to :category, foreign_key: :category_code | |
before_create -> { self.code = Saikoro.alphanumerics(length: 10) } | |
+ # 主キー設定したから不要かも | |
def to_param | |
self.code | |
end | |
diff --git a/http/db/migrate/20150927070129_create_clients.rb b/http/db/migrate/20150927070129_create_clients.rb | |
index 74f3581..4e41f10 100644 | |
--- a/http/db/migrate/20150927070129_create_clients.rb | |
+++ b/http/db/migrate/20150927070129_create_clients.rb | |
@@ -1,7 +1,7 @@ | |
class CreateClients < ActiveRecord::Migration | |
def change | |
- create_table :clients do |t| | |
- t.string :code | |
+ create_table :clients, id: false do |t| | |
+ t.primary_key :code, :string | |
t.timestamps null: false | |
end | |
diff --git a/http/db/migrate/20150927131959_create_categories.rb b/http/db/migrate/20150927131959_create_categories.rb | |
index 8f7b703..db5e176 100644 | |
--- a/http/db/migrate/20150927131959_create_categories.rb | |
+++ b/http/db/migrate/20150927131959_create_categories.rb | |
@@ -1,13 +1,12 @@ | |
class CreateCategories < ActiveRecord::Migration | |
def change | |
- create_table :categories do |t| | |
+ create_table :categories, id: false do |t| | |
+ t.primary_key :code, :string | |
t.string :name, null: false | |
- t.string :code, null: false | |
t.timestamps null: false | |
end | |
add_index :categories, :name, unique: true | |
- add_index :categories, :code, unique: true | |
end | |
end | |
diff --git a/http/db/migrate/20150927132023_create_topics.rb b/http/db/migrate/20150927132023_create_topics.rb | |
index 63102a8..93f4e0b 100644 | |
--- a/http/db/migrate/20150927132023_create_topics.rb | |
+++ b/http/db/migrate/20150927132023_create_topics.rb | |
@@ -1,15 +1,16 @@ | |
class CreateTopics < ActiveRecord::Migration | |
def change | |
- create_table :topics do |t| | |
- t.string :title, null: false | |
- t.string :code, null: false | |
- t.references :category, null: false, index: true, foreign_key: true | |
- t.references :client, null: false, index: true, foreign_key: true | |
- t.integer :posts_count | |
+ create_table :topics, id: false do |t| | |
+ t.primary_key :code, :string | |
+ t.string :title, null: false | |
+ t.string :category_code, null: false, index: true | |
+ t.string :client_code, null: false, index: true | |
+ t.integer :posts_count | |
t.timestamps null: false | |
end | |
- add_index :topics, :code, unique: true | |
+ add_foreign_key :topics, :categories, column: :category_code, primary_key: :code | |
+ add_foreign_key :topics, :clients, column: :client_code, primary_key: :code | |
end | |
end | |
diff --git a/http/db/migrate/20150927132058_create_posts.rb b/http/db/migrate/20150927132058_create_posts.rb | |
index 30ea3f6..2bd128b 100644 | |
--- a/http/db/migrate/20150927132058_create_posts.rb | |
+++ b/http/db/migrate/20150927132058_create_posts.rb | |
@@ -1,11 +1,14 @@ | |
class CreatePosts < ActiveRecord::Migration | |
def change | |
create_table :posts do |t| | |
- t.text :body | |
- t.references :topic, null: false, index: true, foreign_key: true | |
- t.references :client, null: false, index: true, foreign_key: true | |
+ t.text :body | |
+ t.string :topic_code, null: false, index: true | |
+ t.string :client_code, null: false, index: true | |
t.timestamps null: false | |
end | |
+ | |
+ add_foreign_key :posts, :topics, column: :topic_code, primary_key: :code | |
+ add_foreign_key :posts, :clients, column: :client_code, primary_key: :code | |
end | |
end | |
diff --git a/http/db/schema.rb b/http/db/schema.rb | |
index c44d6cf..22eafe8 100644 | |
--- a/http/db/schema.rb | |
+++ b/http/db/schema.rb | |
@@ -16,49 +16,44 @@ ActiveRecord::Schema.define(version: 20150927132058) do | |
# These are extensions that must be enabled in order to support this database | |
enable_extension "plpgsql" | |
- create_table "categories", force: :cascade do |t| | |
+ create_table "categories", primary_key: "code", force: :cascade do |t| | |
t.string "name", null: false | |
- t.string "code", null: false | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
end | |
- add_index "categories", ["code"], name: "index_categories_on_code", unique: true, using: :btree | |
add_index "categories", ["name"], name: "index_categories_on_name", unique: true, using: :btree | |
- create_table "clients", force: :cascade do |t| | |
- t.string "code" | |
+ create_table "clients", primary_key: "code", force: :cascade do |t| | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
end | |
create_table "posts", force: :cascade do |t| | |
t.text "body" | |
- t.integer "topic_id", null: false | |
- t.integer "client_id", null: false | |
- t.datetime "created_at", null: false | |
- t.datetime "updated_at", null: false | |
+ t.string "topic_code", null: false | |
+ t.string "client_code", null: false | |
+ t.datetime "created_at", null: false | |
+ t.datetime "updated_at", null: false | |
end | |
- add_index "posts", ["client_id"], name: "index_posts_on_client_id", using: :btree | |
- add_index "posts", ["topic_id"], name: "index_posts_on_topic_id", using: :btree | |
+ add_index "posts", ["client_code"], name: "index_posts_on_client_code", using: :btree | |
+ add_index "posts", ["topic_code"], name: "index_posts_on_topic_code", using: :btree | |
- create_table "topics", force: :cascade do |t| | |
- t.string "title", null: false | |
- t.string "code", null: false | |
- t.integer "category_id", null: false | |
- t.integer "client_id", null: false | |
+ create_table "topics", primary_key: "code", force: :cascade do |t| | |
+ t.string "title", null: false | |
+ t.string "category_code", null: false | |
+ t.string "client_code", null: false | |
t.integer "posts_count" | |
- t.datetime "created_at", null: false | |
- t.datetime "updated_at", null: false | |
+ t.datetime "created_at", null: false | |
+ t.datetime "updated_at", null: false | |
end | |
- add_index "topics", ["category_id"], name: "index_topics_on_category_id", using: :btree | |
- add_index "topics", ["client_id"], name: "index_topics_on_client_id", using: :btree | |
- add_index "topics", ["code"], name: "index_topics_on_code", unique: true, using: :btree | |
+ add_index "topics", ["category_code"], name: "index_topics_on_category_code", using: :btree | |
+ add_index "topics", ["client_code"], name: "index_topics_on_client_code", using: :btree | |
- add_foreign_key "posts", "clients" | |
- add_foreign_key "posts", "topics" | |
- add_foreign_key "topics", "categories" | |
- add_foreign_key "topics", "clients" | |
+ add_foreign_key "posts", "clients", column: "client_code", primary_key: "code" | |
+ add_foreign_key "posts", "topics", column: "topic_code", primary_key: "code" | |
+ add_foreign_key "topics", "categories", column: "category_code", primary_key: "code" | |
+ add_foreign_key "topics", "clients", column: "client_code", primary_key: "code" | |
end | |
diff --git a/http/test/models/category_test.rb b/http/test/models/category_test.rb | |
index dc5fb2b..df2c602 100644 | |
--- a/http/test/models/category_test.rb | |
+++ b/http/test/models/category_test.rb | |
@@ -1,4 +1,5 @@ | |
require 'test_helper' | |
class CategoryTest < ActiveSupport::TestCase | |
+ should have_many(:topics) | |
end | |
diff --git a/http/test/models/client_test.rb b/http/test/models/client_test.rb | |
index ca872d3..ad39bf2 100644 | |
--- a/http/test/models/client_test.rb | |
+++ b/http/test/models/client_test.rb | |
@@ -1,6 +1,9 @@ | |
require 'test_helper' | |
class ClientTest < ActiveSupport::TestCase | |
+ should have_many :posts | |
+ should have_many :topics | |
+ | |
test ".create" do | |
client = Client.create | |
assert { client.code.size == 16 } | |
diff --git a/http/test/models/post_test.rb b/http/test/models/post_test.rb | |
index 3314ac6..2eee52c 100644 | |
--- a/http/test/models/post_test.rb | |
+++ b/http/test/models/post_test.rb | |
@@ -1,6 +1,9 @@ | |
require 'test_helper' | |
class PostTest < ActiveSupport::TestCase | |
+ should belong_to :client | |
+ should belong_to :topic | |
+ | |
def setup | |
@post = create(:post) | |
end | |
diff --git a/http/test/models/topic_test.rb b/http/test/models/topic_test.rb | |
index 206284d..5fc7965 100644 | |
--- a/http/test/models/topic_test.rb | |
+++ b/http/test/models/topic_test.rb | |
@@ -1,6 +1,10 @@ | |
require 'test_helper' | |
class TopicTest < ActiveSupport::TestCase | |
+ should have_many :posts | |
+ should belong_to :client | |
+ should belong_to :category | |
+ | |
def setup | |
@category = create(:category) | |
@client = create(:client) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment