Rails属于通过关联

时间:2014-06-09 08:56:28

标签: ruby-on-rails activerecord associations

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

现在,用户应该能够对他完成的课程进行评分。 (如果有的话,有一个他的身份证和课程编号的注册。)我认为最好这样写:

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时,我收到以下错误:

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

是否可以这样做?谢谢!

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

<强>结构

也许你太复杂了

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

如果您给rating一个数值(1 - 5),它会让您能够对不同的注册进行评分,如下所示:

user = User.first
course = Course.first

user.enrolments.create(course: course, rating: 5)

-

<强>评分

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

如果您希望ratings courses users包括enrolment(不与join model绑定),您可能希望使用名为{course_ratings #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 1}}或类似的:

user = User.first
user.courses.first.ratings

这将允许您致电:

{{1}}