Ik heb 3 relevante modellen: project, taken en subtaken.
Project heeft veel taken en de taak heeft veel subtaken.
Dienovereenkomstig behoort taak tot project en behoort subtaak tot de taak.
Right now there is no direct connection between project and subtasks. I would like to make one (and have access to things like @some_project.subtasks & @some_subtask.project). I thought to not add the project_id field to subtasks migration, but to use task model as a sort of 'connection' between projects and subtasks (since task already has project_id column and has_many relationship with subtasks).
I started by writing project has_many :subtasks, :through => :tasks and subtask belongs_to :project.
Ik kreeg de subtakenmethode op projectmodel, maar wanneer ik @ some_subtask.project schrijf, wordt ik altijd nihil. Ook als ik scoping gebruik op @ some_project.subtasks krijg ik altijd de foutmelding 'dubbelzinnige kolomnaam'.
Ik denk dat dit is omdat ik mijn relaties verkeerd heb gemaakt. Hoe goed te doen? Ook als het toevoegen van het veld project_id aan het migratiebestand van subtaketten de betere oplossing is (of iets geheel anders) laat het me dan zeker weten.
EDIT: hier zijn de bereiken die kolomnaamfout retourneren (ik heb ze in lib/folder, het is een module die is opgenomen in taak- en subtaskmodellen)
10 # SCOPING
11 def self.included(base)
12
13 today = Date.today
14 start_of_day = DateTime.new(today.year, today.month, today.day, 0, 0, 1)
15 end_of_day = DateTime.new(today.year, today.month, today.day+1, 0, 0, 0)
16
17 base.class_eval do
18 scope :not_targeted, where(:target => nil)
19 scope :targeted, where("target IS NOT ?", nil)
20 scope :targeted_today, where("target > ? AND target < ?", start_of_day, end_of_day)
21 scope :targeted_after_today, where("target > ?", end_of_day)
22 scope :overdue, where("target < ?", start_of_day)
23 end
24
25 end
Those return error when I try to define variables like these in project controller (lines 47 and 51 are to blame for the error). Basically I need these to pass and return proper records.
35 @project_tasks = @project.tasks
36 @project_subtasks = @project.subtasks
45 @today_tasks = @project_tasks.targeted_today
46 @today_subtasks = @project_subtasks.targeted_today
47 @today = @today_tasks + @today_subtasks
48
49 @after_today_tasks = @project_tasks.targeted_after_today
50 @after_today_subtasks = @project_subtasks.targeted_after_today
51 @after_today = @after_today_tasks + @after_today_subtasks
Dit is bijvoorbeeld de foutlijn 47 retourneert ...
SQLite3::SQLException: ambiguous column name: target: SELECT
"subtasks".* FROM "subtasks" INNER JOIN "tasks" ON
"subtasks"."task_id" = "tasks"."id" WHERE "tasks"."project_id" = 1 AND
(target > '2012-06-24 00:00:01' AND target < '2012-06-25 00:00:00')
Hier is het projectmodel:
1 class Project < ActiveRecord::Base
2
3 # RELATIONSHIPS
4 has_many :synapses, :dependent => :destroy
5 has_many :users, :through => :synapses, :dependent => :nullify
6
7 has_many :tasks, :dependent => :destroy
8 has_many :subtasks, :through => :tasks, :dependent => :destroy
9 has_many :discussions, :as => :discussionable, :dependent => :destroy
10
11 #use this when you don't have callbacks
12 #has_many :tasks, :dependent => :delete_all
13
14 # VALIDATIONS
15 validates :name, :presence => true,
16 :length => { :maximum => 50 }
17
18 validates :description, :presence => true,
19 :length => { :maximum => 200, :minimum => 15 }
20
21 # ATTRIBUTE ASSIGNMENT
22 attr_accessible :name, :description
23
24 # CUSTOM METHODS
25 def belonging_project
26 self
27 end
28
29 end
Hier is het taakmodel:
1 class Task < ActiveRecord::Base
2
3 include SharedModelCode
4 # has scoping and validate method
5
6 # RELATIONSHIPS
7 belongs_to :project
8
9 has_many :vesicles, :dependent => :destroy
10 has_many :users, :through => :vesicles, :dependent => :nullify
11
12 has_many :subtasks
13
14 has_many :discussions, :as => :discussionable, :dependent => :destroy
15
16 # VALIDATIONS
17 validates :project, :presence => true
18 validates :name, :presence => true,
19 :length => { :maximum => 50 }
20 validates :description, :presence => true,
21 :length => { :maximum => 200, :minimum => 15 }
22 validate :target_date_cannot_be_in_the_past
23
24 # ATTRIBUTE ASSIGNMENT
25 attr_accessible :name, :description, :target, :completed
26
27 # CUSTOM METHODS
28 def belonging_project
29 Project.find_by_id(self.project_id)
30 end
31
32 end
Hier is het Subtask-model:
1 class Subtask < ActiveRecord::Base
2
3 include SharedModelCode
4 # has scoping and validate method
5
6 # RELATIONSHIPS
7 belongs_to :task
8 belongs_to :project
9
10 has_many :subvesicles, :dependent => :destroy
11 has_many :users, :through => :subvesicles, :dependent => :nullify
12
13 has_many :discussions, :as => :discussionable, :dependent => :destroy
14
15 # VALIDATIONS
16 validates :name, :presence => true
17 validates :task, :presence => true,
18 :length => { :maximum => 200 }
19 validate :target_date_cannot_be_in_the_past
20
21 # ATTRIBUTE ASSIGNMENT
22 attr_accessible :name, :target, :completed
23
24 # CUSTOM METHODS
25 def belonging_project
26 task = Task.find_by_id(self.task_id)
27 Project.find_by_id(task.project_id)
28 end
29
30 end