Rails 5 - Attribute update if relative model's attribute is different -


i've created attribute supposed update datetime field value of child model's attribute if it's newer (or older based on field)

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 

it's messy, working within app.

a client has many projects has many tasks , client has many tasks through projects

however, when try seed db seeing output similar to:

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 

which seems it's pulling client not updating record? (unsure how test theory).

i went app , make client -> project -> task user , records if expect, seems seeding ignores function, updating project portion.

seed file

task_list = [   ['task 1', 'nothing add', '2017-09-01', '2018-01-01', 1, 1, 0],     ['task 3', 'nothing add', '2017-08-01', '2017-10-01', 1, 2, 1],   ['task 4', 'nothing add', '2017-06-01', '2017-09-01', 1, 3, 1],   ['task 5', 'nothing add', '2017-11-01', '2017-12-01', 1, 4, 0],   ['task 6', 'nothing add', '2017-08-01', '2018-02-01', 1, 5, 0],   ['task 7', 'nothing add', '2017-08-01', '2017-12-01', 1, 6, 0],   ['task 8', 'nothing add', '2017-11-01', '2017-12-15', 1, 7, 0],   ['task 9', 'nothing add', '2017-07-01', '2017-08-15', 1, 1, 0],   ['task 17', 'nothing add', '2017-04-01', '2017-08-01', 1, 6, 1],   ['task 28', 'nothing add', '2017-08-01', '2017-10-01', 1, 7, 0],   ['task 39', 'nothing add', '2018-01-01', '2018-04-01', 1, 1, 1] ]  task_list.each |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 

i didn't have project assignment line seeing if perhaps resolved mia client details.

tl;dr

if task created , has later end_date client's furthest_project_end datefield, should update field. not updating. updating project.end_date


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -