1

I am writing a todo app, which track often repeated tasks. I have a CheckList model which has_many Tasks within it. Users can create a new CheckList and add Task to it as you normally would in Rails.

I would like users to also be able to create a checklist template (which would have its own Template model). This Template would be a representation (I'm thinking in JSON) of a CheckList and its Tasks, and I would like to be able to create a new CheckList and populate it's Tasks from it. That way they can just recreate the same checklist at different times.

Is there an easy way to accomplish this?

Is there any way to create a create_list_from_template method somewhere that would parse the template and create the CheckList and Tasks accordingly?

Am I going about this all wrong? Any suggestions appreciated...

EDIT

I will use NestedAttributes to create the tasks directly from a CheckList and that is fine. What I am specifically asking, is how can I create a CheckList from a Template.

How would I call and pass the creation of this new CheckList based on the JSON data contained in the Template model?

Is there a way to create a different new action on the CheckListsController, to which I could pass the Template data? Like a new_checklist_from_template action or something?

1 Answer 1

2

You should checkout NestedAttributes. It will allow you to create parent object and n number of child objects in one go. In your case parent is CheckList and children are tasks.

Check the following link

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Sign up to request clarification or add additional context in comments.

3 Comments

That is awesome, thanks. But how would I go about using that from the Template controller/model? Do I have to call a function that creates a new CheckList from the Template Controller? What action should that be?
There are many examples showing how to use Nested Attributes on the web. Here is the simplest example. coderwall.com/p/qwx3qa/nested-form-and-rails-4
To answer your question, you have to create new Checklist and pass the parameters for tasks as tasks_attributes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.