Rails属于通过关联

我正在尝试将新模型添加到现有模型网格中。 现有的工作完美,但我不能让新工作正常工作,我想知道协会是否能够以我试图使其工作的方式工作。 更新:我刚刚被问到: 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的注册。)我认为最好写如下:

 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 insted使用has_one through ,我收到以下错误:

 ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'User#ratings' because the source reflection class 'Rating' is associated to 'Enrolment' via :has_one. 

有可能这样做吗? 谢谢!

结构体

也许你太复杂了

 class Enrollment belongs_to :course belongs_to :user end 

这意味着您有一个连接模型,它存储唯一的记录,引用courseuser 。 您的ratings是按usercourse吗?

为什么不将rating作为enrolment模型的属性包括在内?:

 #enrolments id | user_id | course_id | rating | created_at | updated_at 

如果您给出一个数字值(1 – 5),它将使您能够对不同的注册进行rating ,如下所示:

 user = User.first course = Course.first user.enrolments.create(course: course, rating: 5) 

评级

当然,这是基于您当前的模型结构。

如果您想要包含userscoursesratings (与enrolment无关),您可能希望使用名为course_ratings或类似的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 
 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 

注意:添加foreignkey列

如果您在评级表中只有一两列将它们合并到这样的注册中。

 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