[英]Rails belongs_to through association
我正在嘗試將新模型添加到現有模型網格中。 現有的工作正常,但是我無法使新的工作正常,並且想知道該協會是否能夠按照我試圖使其正常工作的方式工作。 更新:正如我剛被問到的那樣:我在解決這個問題時已經讀過《 belongs_to through
。 如果不存在, has_one through
將是正確的方法嗎? 我也嘗試過,但是也沒有用。
這是現有的網格:
class Course
has_many :users, through: :enrollments
has_many :enrollments
end
class User
has_many :courses, through: :enrollments
has_many :enrollments
end
class Enrollment
belongs_to :course
belongs_to :user
# has fields :user_id, :course_id
end
現在,用戶應該能夠對他完成的課程進行評分。 (如果他有,那么會有一個帶有他的ID和一個課程ID的注冊。)我認為最好這樣寫:
class Course
has_many :users, through: :enrollments
has_many :enrollments
has_many :ratings, through: :enrollments
end
class User
has_many :courses, through: :enrollments
has_many :enrollments
has_many :ratings, through: :enrollments
end
class Enrollment
belongs_to :course
belongs_to :user
has_one :rating
# has fields :user_id, :course_id
end
class Rating
belongs_to :enrollment
belongs_to :course, through: :enrollment
belongs_to :user, through: :enrollment
end
當我嘗試在控制台中創建評級時,出現以下錯誤:
User.first.ratings.create(text: "test", course_id: Course.first.id)
ArgumentError: Unknown key: through
更新當我has_one through
插入使用has_one through
,出現以下錯誤:
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'User#ratings' because the source reflection class 'Rating' is associated to 'Enrolment' via :has_one.
完全可以這樣做嗎? 謝謝!
class Course
has_many :users, through: :enrollments
has_many :enrollments
has_many :ratings, through: :enrollments
end
class User
has_many :courses, through: :enrollments
has_many :enrollments
has_many :ratings, through: :enrollments
end
class Enrollment
belongs_to :course
belongs_to :user
belongs_to :rating
# has fields :user_id, :course_id, rating_id
end
class Rating
has_one :enrollment
has_one :course, through: :enrollment
has_one :user, through: :enrollment
end
注意:添加外鍵列
如果您在評分表中只有一兩列,則將它們合並為這樣的注冊。
class Course
has_many :users, through: :enrollments
has_many :enrollments
end
class User
has_many :courses, through: :enrollments
has_many :enrollments
end
class Enrollment
belongs_to :course
belongs_to :user
# has fields :user_id, :course_id, rating-columns...
end
結構體
也許你把這個復雜化了
class Enrollment
belongs_to :course
belongs_to :user
end
這意味着您有一個聯接模型,該模型存儲唯一的記錄,同時引用course
和user
。 您的ratings
是基於每個user
和course
?
您為什么不只將rating
作為enrolment
模型的屬性?
#enrolments
id | user_id | course_id | rating | created_at | updated_at
如果您給數字rating
(1-5),則可以對不同的入學情況進行評分,如下所示:
user = User.first
course = Course.first
user.enrolments.create(course: course, rating: 5)
-
等級
當然,這是基於您當前的模型結構。
如果要包括users
對courses
的ratings
(不與enrolment
掛鈎),則可能希望使用稱為course_ratings
或類似的join model
:
#app/models/user.rb
Class User < ActiveRecord::Base
has_many :enrolments
has_many :courses, through: :enrolments
has_many :ratings, through: :courses, class_name: "CourseRating"
end
#app/models/course.rb
Class Course < ActiveRecord::Base
has_many :enrolments
has_many :students, through: :enrolments, class_name: "User"
has_many :ratings, class_name: "CourseRating"
end
#app/models/course_rating.rb
Class CourseRating < ActiveRecord::Base
belongs_to :course
belongs_to :user
end
這將使您可以致電:
user = User.first
user.courses.first.ratings
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.