# INSERT a rercord in another model

**URL:** <https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589>\
**Category:** Rails\
**Created:** [May 4, 2008, 2:17am UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589 "2008-05-04T02:17:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [May 4, 2008, 2:17am UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589/1 "2008-05-04T02:17:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [May 4, 2008, 2:32am UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589/2 "2008-05-04T02:32:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [May 4, 2008, 2:39am UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589/3 "2008-05-04T02:39:13Z")

</div>

the second option…

On 3 maio, 21:32, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [May 5, 2008, 6:43am UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589/4 "2008-05-05T06:43:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [May 4, 2008, 4:12am UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589/5 "2008-05-04T04:12:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [May 5, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589/6 "2008-05-05T23:05:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [May 6, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589/7 "2008-05-06T18:12:28Z")

</div>

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](http://railscasts.com). There was a decent screencast on it  
recently.

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [May 6, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/insert-a-rercord-in-another-model/136589/8 "2008-05-06T16:36:16Z")

</div>

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.
