Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

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

General • Asked by John Munyi

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 ?


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


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


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?


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

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.


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


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.