Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I implement a model association that when changed older records are not affected

John Munyi asked in General

Here are my models (User , Project and Task) A user has many projects , a project has many users , A project has many tasks and a tasks belongs to a project , a user has many tasks and a task belongs to user .

I am using the above to automatically capture time taken per task , the user has to activate a timer when he / she start a start and stops it at the end of task and the time is automatically recorded.....All that is working fine and the time stamps are well recorded.

The only issue is if i switch a user to a different project (e.g project ABC) even the earlier times recorded under project XYZ get updated to read ABC.... how can i get around that ?

Reply

How are you associating the tasks to the project and what code do you use to switch them between projects?

Reply

A task belongs to a domain and domain belongs to a project , there is no direct association between project and task.....

Another thing i can point out is i have introduced an activity which belongs to a user and also to a task. This seem to work better they are all associated via ids ..... so now i can record a duration of a user who has worked in different projects and store it even if he moved from one project to another

but now my next question is how do i display the real name rather than the IDs ? on my view

Reply

I'm not entirely sure I'm following. What's the domain object for? And the problem you're having is trouble rendering the task's project name? Does something like <%= @task.domain.project.name %> work?

Reply

yes Its has worked thanks, dont about the domain , it just explains the context which is needed as for now.

I am just wondering if there would be a cleaner way to write this line

        td= activity.task.domain.project.code
Reply

You might be able to add an association to the task like has_one :project, through: :domain to remove the domain out of it, but it's not going to make much difference. Shortening this only really moves this code somewhere else.

Reply

Then for now no need..... It's working so I will let it be

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.