2016-07-28 52 views
0

爲了更簡單的我有外鍵導軌,如何添加?

class User 
    has_many :questions, trough: votes 
    has_many :questions  #(as the author) 
    has_many :votes 
end 

忘記創建時添加foreign_key,現在我不知道如何將它添加到特定的(通過的has_many)關聯

schema.rb

enable_extension "plpgsql" 

    create_table "answers", force: :cascade do |t| 
    t.text  "body" 
    t.datetime "created_at",     null: false 
    t.datetime "updated_at",     null: false 
    t.integer "question_id" 
    t.integer "user_id" 
    t.boolean "best",  default: false 
    end 

    add_index "answers", ["question_id"], name: "index_answers_on_question_id", using: :btree 
    add_index "answers", ["user_id"], name: "index_answers_on_user_id", using: :btree 

    create_table "attachments", force: :cascade do |t| 
    t.string "file" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.integer "attachable_id" 
    t.string "attachable_type" 
    end 

    add_index "attachments", ["attachable_id", "attachable_type"], name: "index_attachments_on_attachable_id_and_attachable_type", using: :btree 

    create_table "questions", force: :cascade do |t| 
    t.string "title" 
    t.text  "body" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "user_id" 
    end 

    add_index "questions", ["title"], name: "index_questions_on_title", using: :btree 
    add_index "questions", ["user_id"], name: "index_questions_on_user_id", using: :btree 

    create_table "users", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.inet  "current_sign_in_ip" 
    t.inet  "last_sign_in_ip" 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree 
    add_index "users", ["name"], name: "index_users_on_name", unique: true, using: :btree 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree 

    create_table "votes", force: :cascade do |t| 
    t.integer "votable_id" 
    t.string "votable_type" 
    t.integer "user_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "votes", ["user_id", "votable_id"], name: "index_votes_on_user_id_and_votable_id", unique: true, using: :btree 

    add_foreign_key "answers", "questions", on_delete: :cascade 
    add_foreign_key "questions", "users", on_delete: :cascade 
end 
+0

你可以發佈你的'DB/schema.rb'? – siegy22

+0

您的意思是user_id在其他模型中 –

+0

RaVeN'code'create_table「votes」,force::cascade do | t | t.integer「votable_id」 t.string「votable_type」 t.integer「user_id」 t.datetime「created_at」,null:false t。datetime「updated_at」,null:false 結束 –

回答

0

運行控制檯命令

rails g migration AddForeignKeyToVotes user:references question:references 

這將產生db/migrate/xxxx_add_foreign_key_to_votes.rb文件,內容如下

class AddForeignKeyToVotes < ActiveRecord::Migration 
    def change 
    add_reference :votes, :user, index: true, foreign_key: true 
    add_reference :votes, :question, index: true, foreign_key: true 
    end 
end 
+0

事情是我需要一個有問題的外鍵。我是初學者,可能是我不明白的。但是這個添加的目的是爲別名的原因,我必須has_many:問題,我想添加一個命名的外鍵來將它們分開 –

0

你真正需要的外鍵?

許多Rails開發人員對Rails處理應用程序中的關係而不是數據庫的方式感到滿意。

爲您的情況:

class User 
    has_many :questions, trough: votes 
    has_many :questions  #(as the author) 
    has_many :votes 

如果votes表有question_iduser_id就足以定義的關係沒有任何外鍵,除非你有一個原因,確實需要這樣的外鍵被定義的數據庫級別。

仔細閱讀THIS SECTION,Rails正在使用Convention over Configuration

作爲一個小例子:您的User模型如何知道要查詢哪個表並檢索數據,而沒有任何配置,它將搜索具有相同名稱users(約定)的表並使用它,對於外鍵也是如此。

根據您的評論你有一個模型的東西爲#1,你有一個User可以請Question,可以回答在這種情況下,你可能有一些像Question誰:

class User 
    has_many :asked_questions, class_name: 'Question' # user can ask many questions 
    has_many :voted_questions, through: :votes, source: 'question' 
    has_many :votes # to get all votes this user did 
end 

class Question 
    has_many :votes # to get all votes for a question 
    belongs_to :user 
end 

class Vote 
    belongs_to :user 
    belongs_to :question 
end 

數據庫將是什麼像:

# Table User 
# Table Question (user_id) 
# Table Vote (user_id, question_id) 

比方說,你想獲得Questions用戶問他們這將是:

如果你想獲得 Questions誰的用戶票
user = User.first 
user.asked_questions 

user.voted_questions 
+0

mohamed-ibrahim我需要這個來解決分離問題。可能是我走錯了路,但我想作爲作者和作爲投票人的問題解決問題/這些是不同的協會 –

+0

@ anti-k檢查我的更新答案 –

+0

mohamed-ibrahim非常感謝你! –