之間的多重關係,我有我的應用程序的現有數據庫遷移,它包含以下內容:導軌 - 兩種型號
class User < ApplicationRecord
has_many :courses
end
class Course < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :locations
has_and_belongs_to_many :categories
end
每個用戶都可以投一次爲一個療程或者like
或dislike
,所以我創建了一個加盟表中db/migrate
:
class CreateUsersCourses < ActiveRecord::Migration[5.0]
def change
create_table :users_courses do |t|
t.integer :course_id, null: false
t.integer :user_id, null: false
end
end
現在我不知道如何在User
和Course
模型,並不能使它與一人重疊添加新關係y關係。
每個「課程」只能屬於一個「用戶」嗎? – Iceman
是的,因爲這裏的情況是'用戶'創建'課程'。現在我想做'用戶'投票'課程',不知道如何添加新的關係。 –
難道你不能像那樣只是你的「課程」模型中的一個專欄嗎?這似乎是非正統的,但它是最簡單的解決方案,如果'課程'曾經只屬於一個'用戶' – Iceman