handling order with has_many through relationship

徘徊边缘 提交于 2019-12-09 16:24:18

问题


I have two models: project and task (for example) with a join model: project_task enabling a has_many through relationship so that tasks may be shared across projects.

I have specified position as an attribute of the project_task model. Now I want to be able to access tasks by their position in the project_tasks table via a given project.

i.e. project.tasks (ordered by the position listed for each task in the project_tasks table).

Is this possible?


回答1:


I think something like that can help you:

has_many :project_tasks
has_many :tasks, :through => :project_tasks, :order => 'project_tasks.position'



回答2:


class Task < AR::Base
   belongs_to :project
   has_one :project_tasks,:through=>:project_tasks
end

class Project < AR::Base 
  has_many :project_tasks
  has_many :tasks ,:through=>:project_tasks,:order => 'project_tasks.position'
end

class ProjectTask < AR::Base
  belongs_to :task
  belongs_to :project
end


来源:https://stackoverflow.com/questions/6017449/handling-order-with-has-many-through-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!