Ruby on rails RubyonRails-访问引用的联接表';s对象
评级.rbRuby on rails RubyonRails-访问引用的联接表';s对象,ruby-on-rails,ruby,Ruby On Rails,Ruby,评级.rb class Rating < ActiveRecord::Base belongs_to :vote belongs_to :student belongs_to :course_textbook validates_presence_of :vote, :student end class CourseTextbook < ActiveRecord::Base belongs_to :course belongs_to :textbook
class Rating < ActiveRecord::Base
belongs_to :vote
belongs_to :student
belongs_to :course_textbook
validates_presence_of :vote, :student
end
class CourseTextbook < ActiveRecord::Base
belongs_to :course
belongs_to :textbook
has_many :ratings
validates_presence_of :course, :textbook
end
等级
课程\u教科书.rb
class Rating < ActiveRecord::Base
belongs_to :vote
belongs_to :student
belongs_to :course_textbook
validates_presence_of :vote, :student
end
class CourseTextbook < ActiveRecord::Base
belongs_to :course
belongs_to :textbook
has_many :ratings
validates_presence_of :course, :textbook
end
class CourseTextbook
有没有一种方法可以代替评分。课程\课本。课本你去评分。课本?我会在模型中创建方法吗?或者有没有办法让ActiveRecord做到这一点
谢谢您可以通过授权来实现:
class Rating < ActiveRecord::Base
...
delegate :textbook, to: :course_textbook
end
您可以在此处找到
委托
-方法的更多选项:太好了,谢谢!我知道rails会有所收获的。非常感谢。如果允许的话,我一定会接受这个答案。