Processing order with has_many through relationships

I have two models: a project and a task (for example) with a connection model: project_task, which allows has_many through relationships so that tasks can be shared in projects.

I specified the position as an attribute of the project_task model. Now I want to have access to tasks by their position in the project_tasks table through this project.

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

Is it possible?

+7
source share
2 answers

I think something like this might help you:

has_many :project_tasks has_many :tasks, :through => :project_tasks, :order => 'project_tasks.position' 
+17
source
 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 
+2
source

All Articles