我刚开始使用Rails,所以这可能是一个非常愚蠢的问题,但我花了很多时间在谷歌上搜索,但仍然没有弄清楚。我创造了这些脚手架:
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在我看来,如何访问父记录(当然)的属性?
发布于 2015-02-27 19:17:27
只需使用.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 1https://stackoverflow.com/questions/28772575
复制相似问题