Nested form and models?
I'm working on an app that will track medication. I have a controller called
narcotic_usage.rb which will be a simple form to note what narcotics are being used, their expiration dates, etc.
My original thought was to simply create a model called NarcoticUsage and include the drugs in their as
drug_1_expiration etc etc. But I think this would create a huge model unnecessarily.
Instead I'm thinking of having a nested form/model like so:
What I want to be able to do is create a new narcotic usage object, and be able to dynamically add multiple drugs into the form. So someone could add 1-2 drugs to their narcotic usage report or they could add 50.
Considering the association, what is the best way to go about this? Or should I look at a has_many through type of relationship?
I'm familiar with nested_forms and attributes but am kind of unsure on the design aspect of this feature and which route to go.
If I can provide additional information or code snippets, please let me know.
This seems alright to me. Have you looked into the cocoon gem for making the form?
I just took a look at the cocoon gem. That really makes nested forms easier. I'll give it a shot. My main concern was what's the best approach to take on the model side with relationships.
I think the has_many relationship seemed good. Anything more complex sounded like it would be overkill unless you've got some other requirements to add in there.
I think you're right. A has_many should do the trick. Just trying to figure out how to create new Narcotics from within the nested form. Seems like I have to do a bit of research to make this work. Unless you have a tutorial out there that explains it well. I've used nested forms for attributes already existing in the db, haven't used it for creating new associated objects.