如何指定Has_many订单

时间:2014-08-01 03:45:31

标签: ruby-on-rails-3 has-many

如何在Has_many关系中分配特定订单?

class GolfCourse < ActiveRecord::Base
  has_many :course_nines
end

class CourseNine < ActiveRecord::Base
  belongs_to :golf_courses
end

每个GolfCourse将有两个CourseNines,我想指定/分配一个CourseNine作为first/front和一个second one作为second/back

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

class GolfCourse < ActiveRecord::Base
  has_many :course_nines do
    def front_nine
      order("created_at asc").first() #or whatever logic you want
    end
    def back_nine
      order("created_at desc").first() #or whatever logic you want
    end
  end
end

这个例子假设你每个GolfCourse都有2个NineCourses。还有其他方法可以做到,但你需要提供一些你想做的更多细节