我已经创建了一个属性,该属性应该使用子模型属性的值更新日期时间字段(如果它更新(或更旧基于哪个字段)
def update_project_record
client = Client.find(project.client_id)
puts client
project.start_date = self.task_start_date if self.task_start_date < project.start_date
project.end_date = self.task_end_date if self.task_end_date > project.end_date
if client.nearest_project_start.present?
client.assign_attributes(:nearest_project_start => self.task_start_date, :nearest_project_id => self.project_id) if self.task_start_date < client.nearest_project_start
else
client.assign_attributes(:nearest_project_start => self.task_start_date, :nearest_project_id => self.project_id)
end
if client.furthest_project_end.present?
client.assign_attributes(:furthest_project_end => self.task_end_date, :furthest_project_id => self.project_id) if self.task_end_date > client.furthest_project_end
else
client.assign_attributes(:furthest_project_end => self.task_end_date, :furthest_project_id => self.project_id)
end
client.save
end
它很混乱,但它在应用程序中运行。
Client
有许多Projects
,其中包含许多Tasks
和Client has many Tasks through Projects
然而,当我尝试播种数据库时,我看到的输出类似于:
SDK Enhancement
#<Client:0x007fa89d33bda8>
#<Client:0x007fa89d31a108>
Task 1
#<Client:0x007fa89d2c1580>
#<Client:0x007fa89d2a0880>
Task 3
#<Client:0x007fa89d2488b0>
#<Client:0x007fa89d21bae0>
Task 4
#<Client:0x007fa89d1bfbc8>
#<Client:0x007fa89d19eec8>
Task 5
#<Client:0x007fa89d142e98>
#<Client:0x007fa89d11e0c0>
Task 6
#<Client:0x007fa89d0c1f78>
#<Client:0x007fa89d0a1278>
Task 7
#<Client:0x007fa89d049208>
#<Client:0x007fa89d024430>
Task 8
#<Client:0x007fa89cfcc3c0>
#<Client:0x007fa89cfa3588>
Task 9
#<Client:0x007fa89cf4b478>
#<Client:0x007fa89cf2a5c0>
Task 17
#<Client:0x007fa89ced2438>
#<Client:0x007fa89ceb1670>
Task 28
#<Client:0x007fa89ce59600>
#<Client:0x007fa89ce348a0>
Task 39
这似乎是在拉客户端而不是更新记录? (不确定如何测试这个理论)。
然后我进入了应用程序并创建了一个客户端 - &gt;项目 - &gt;作为用户的任务和所有记录看起来就像我期望的那样,但似乎Seeding忽略了这个功能,但是它正在更新Project
部分。
种子档案
task_list = [
['Task 1', 'Nothing to add', '2017-09-01', '2018-01-01', 1, 1, 0],
['Task 3', 'Nothing to add', '2017-08-01', '2017-10-01', 1, 2, 1],
['Task 4', 'Nothing to add', '2017-06-01', '2017-09-01', 1, 3, 1],
['Task 5', 'Nothing to add', '2017-11-01', '2017-12-01', 1, 4, 0],
['Task 6', 'Nothing to add', '2017-08-01', '2018-02-01', 1, 5, 0],
['Task 7', 'Nothing to add', '2017-08-01', '2017-12-01', 1, 6, 0],
['Task 8', 'Nothing to add', '2017-11-01', '2017-12-15', 1, 7, 0],
['Task 9', 'Nothing to add', '2017-07-01', '2017-08-15', 1, 1, 0],
['Task 17', 'Nothing to add', '2017-04-01', '2017-08-01', 1, 6, 1],
['Task 28', 'Nothing to add', '2017-08-01', '2017-10-01', 1, 7, 0],
['Task 39', 'Nothing to add', '2018-01-01', '2018-04-01', 1, 1, 1]
]
task_list.each do |name, comment, start, end_date, project, product, completed|
dproject = Project.find(project)
dproject.tasks.create!(task_name: name, comment: comment, task_start_date: start, task_end_date: end_date, project_id: project, product: product, completed: completed)
puts name
end
我原本没有项目分配线,但我看到是否解决了MIA客户端的详细信息。
铊组成; dr
如果创建了一个任务,并且其日期end_date
比客户的furthest_project_end
日期字段晚,则应更新该字段。目前尚未更新。然而,它正在更新project.end_date