Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6384969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:50:27+00:00 2026-05-25T02:50:27+00:00

So I have a Task model, a Ticket model and a Category model. Category

  • 0

So I have a Task model, a Ticket model and a Category model.

Category has_many Tasks
Ticket has_many Tasks
Category has_many Tickets
Ticket belongs_to Category
Task belongs_to Ticket
Task belongs_to Category

The Tasks that are associated to a Category are the default Tasks for a Ticket when the Category is linked to a Ticket. So I need the Tasks that are associated to a Category to be duplicated and associated to the Ticket when a Category is selected for the Ticket.

I’m wondering if I can do something like this in my Ticket model:

  after_create :duplicate_tasks_to_ticket
  after_update :duplicate_tasks_to_ticket

  def duplicate_tasks_to_ticket
    if self.tasks.blank?
      for task in self.category.tasks.all
        new_task = Task.new
        new_task.name = task.name
        new_task.ticket_id = self.id
        new_task.save
      end
    end
  end

Right now this doesn’t throw any errors but it doesn’t do anything. Any help would be greatly appreciated. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:50:27+00:00Added an answer on May 25, 2026 at 2:50 am

    If you are already associating them to the category, why do they need to be associated to the ticket also? If you need the granularity of being tied to a ticket, then just use that association to get all tasks under a category. Tieing them to both just make site complicated. Now, if you absolutely need to tie them to both, you should create an assignment table for that, which stores category_id, task_id. Another assignment table for ticket_id, task_id. Then keep the task as one record, as supposed to duplicating data.

    Your code looks fine for the most part. I don’t think you need the .all at the end. The associations will automatically load all that you are referencing under that model.

    Though try using the association build method instead:

    def duplicate_tasks_to_ticket
      if self.tasks.blank?
        for task in self.category.tasks.all
          self.tasks.build(:name => task.name)
        end
        self.save
      end
    end
    

    The build creates a new object of that association, assigning the foreign key for you. Then you just call save on the parent model, and it will do all the proper insert statements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my User model I could have: has_many :tasks and in my Task model:
Say I have a User model, a Task model that belongs_to :user , :has_one
[Updated a bit] I have a Task model that has a :completed boolean attribute.
I have two models Project and 'Task` where project has_many tasks and task belongs
I have a Task model where every task has deadline. class Tasks(models.Model): assigned_to =
i have completely done all the task that is Model generator Crud generator after
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a model Task which will HABTM many TaskTargets. When it comes to
I have the fllowing requirement, I have a model called Task to display user
I have a Task object that has a collection of Label objects ... in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.