[英]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.