我是Rails的新手,所以这可能是一个非常愚蠢的问题,但是我花了很多时间在Google上搜寻,但仍然没有弄清楚。我创建了这些脚手架:
rails g scaffold Course coursename:string program:string
rails g scaffold Unit unitname:string daybegins:integer durationdays:integer course:references
我的模型是:
class Course < ActiveRecord::Base
validates :coursename, presence: true, :uniqueness => {:case_sensitive => false}
has_many :units
end
class Unit < ActiveRecord::Base
belongs_to :course
end
我如何查看我的父记录(课程)的属性?
只需使用.course
。因此,如果您在Rails控制台中并想找到1号单元(或您选择的任何单元)的课程,它将看起来像:
unit = Unit.find(1)
unit.course # will display the course for unit 1
unit.course.coursename # will display the course name for unit 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句