INSERT a rercord in another model

Hi all,

i have the following: In the products controller, i need to insert (if it doesn´t exist) the supplier_id in the products table. The reason for this is that when a user is entering a new product, and there isn´t a supplier listed in the select list, it must be included "in the same form".

am really lost on how to do that.

flaubert wrote:

Hi all,

i have the following: In the products controller, i need to insert (if it doesn�t exist) the supplier_id in the products table. The reason for this is that when a user is entering a new product, and there isn�t a supplier listed in the select list, it must be included "in the same form".

am really lost on how to do that.

Is your problem that you do not have all of your valid supplier ids listed in the select list on the product/new view or that the supplier does not exist in the control table (suppliers for instance)? These are two very different problems.

the second option...

I take it that the person entering the product data is not necessarily aware that the supplier they are using is not already on file. The question is, how does the system determine that instance in your design? In other words, what is entered on the product screen that would tell the controller that the vendor supplier is not yet on file. Row IDs are typically auto-assigned so it seems a dubious proposition that entering a supplier_id not already in the suppliers table is the trigger.

yeah if they enter a supplier that doesn't exist then create it later [in the action] or immediately using ajax, I'd imagine. -R

i want to create the supplier immediately using ajax, but how?? Where can I found some examples? what about the supplier form inside the product form?

flaubert wrote:

i want to create the supplier immediately using ajax, but how?? Where can I found some examples? what about the supplier form inside the product form?

I think that it would be useful to step back in the process and sketch out what the user has to work with and how this is presented to the system. Questions that come to mid for me are:

How come the product data to be entered? Is this upon receipt of goods or in anticipation of an order? How does one come to possess or order goods from an previously unknown supplier? Is there a procedural problem that needs addressing in the business model first?

What supplier information does the user possess? What is unique to the supplier? Are supplier codes separate from the surrogate id value assigned? If so then by whom and how are they controlled? If codes are not used then how are duplicate supplier entries prevented? Must the supplier name be unique? Do we have address information? Accounting information? Federal and state tax numbers? Contact information?

Before one decides on what and how to add supplier records a bit of time spent on determining on when, why and who seems in order.

If your user has the name of the supplier but the supplier does not exist in the suppliers table, you might be able to use autocomplete. Check out http://railscasts.com. There was a decent screencast on it recently.