# Is there a more efficient way to work dropdowns from other t

**URL:** <https://www.ruby-forum.com/t/is-there-a-more-efficient-way-to-work-dropdowns-from-other-t/146476>\
**Category:** Rails\
**Created:** [August 21, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/is-there-a-more-efficient-way-to-work-dropdowns-from-other-t/146476 "2008-08-21T15:38:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/is-there-a-more-efficient-way-to-work-dropdowns-from-other-t/146476/1 "2008-08-21T15:38:22Z")

</div>

Hi, all.

Just wondering if there is a better dry way to do this:

I have an inventory program, that I’m writing to learn rails. It has  
multiple tables, one of the main ones is called parts. Parts has name,  
manufacturer, vendor, etc columns.  
Manufacturer and vendor columns are just storing ids of records from  
tables Manufacturers and Vendors. So when I’m creating a form in the  
view for “new” action with the dropdowns I need to first get data from  
those two table in my controller then iterate in my view to show all the  
options in the dropdown. This is all fine. My question is - when I save  
the form it complains that I have no @vendors and @manufacturers in my  
create method. So I have to do @vendors = Vendor.find(:all) again.

It’s not a big performance hit, since I only might have potentially 50  
records in each table, but it seems to me I should be able to do  
@vendors = Vendor.find(:all) once in my controller and then use that in  
many actions.

Please advise. Below are snippets from my code (please excuse crudity):

Controller

Code :

1. def new
2. @vendors = Vendor.find(:all)
3. @manufacturers = Manufacturer.find(:all)
4. @departments = Department.find(:all)
5. part\_status\_type = StatusType.find\_by\_name(“part status”).id
6. @part\_statuses =  
Status.find\_all\_by\_status\_type(part\_status\_type)
7. @part = Part.new
8. end
9. 
10. def edit
11. end
12. 
13. def create
14. 

```
@vendors = Vendor.find(:all)

```

15. @manufacturers = Manufacturer.find(:all)
16. @departments = Department.find(:all)
17. part\_status\_type = StatusType.find\_by\_name(“part status”).id
18. @part\_statuses =Status.find\_all\_by\_status\_type(part\_status\_type)
19. @part = Part.new(params[:part])
20. if @part.save
21. 

```
flash[:notice] = 'Part with id <strong>#' + @part.id.to_s +

```

’ added successfully’  
22. redirect\_to parts\_path  
23. else  
24. render :action =\> ‘new’  
25. end  
26. end

View

Code :

1. 
2. Vendor
3. Vendor part #
4. 
5. 
6. 
7. 
8. 

```
<%= collection_select("part", "vendor", @vendors, "id",

```

“name”) %\>  
9.

  
10. \<%= f.text\_field :vendor\_specific\_id %\>  
11.   
12.   
13. Manufacturer  
14. Manufacturer part #  
15.   
16.   
17.   
18. \<%= collection\_select(“part”, “manufacturer”, @manufacturers,  
“id”, “name”) %\>  
19.   
20. \<%= f.text\_field :manufacturer\_specific\_id %\>  
21.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/is-there-a-more-efficient-way-to-work-dropdowns-from-other-t/146476/2 "2008-08-21T16:09:29Z")

</div>

def create  
@part = Part.new(params[:part])  
if @part.save  
flash[:notice] = ‘Part with id **#’ + @part.id.to\_s + ‘\</  
strong\> added successfully’  
redirect\_to parts\_path  
else  
 @vendors = Vendor.find(:all)  
 @manufacturers = Manufacturer.find(:all)  
 @departments = Department.find(:all)  
part\_status\_type = StatusType.find\_by\_name(“part status”).id  
 @part\_statuses =Status.find\_all\_by\_status\_type(part\_status\_type)  
render :action =\> ‘new’  
end  
end**

The reason for this is the render :action =\> 'new business. You see,  
when you create a part, there are 2 ways to go; either you are  
succesfull or not. If you are then everything is groovy and you  
redirect\_to wherever, the redirect\_to, disposes of all your variables  
(@part, @vendors…) and calls the new action method as if it was the  
first time. If its not succesfull however, we cant redirect\_to because  
wed loose all the data that the user put in in the first place and  
restart with a blank form. So we render, which means, grab the view,  
and render it as if i was that method but do not call the method. that  
is why you need to initialize the variables again, thats why ive put  
them in the else statement, so that they only get loaded if you are  
not succesfull.

try this, remove the “duplicate” varianble assignments and create a  
part that is succesfull, it wont complain.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/is-there-a-more-efficient-way-to-work-dropdowns-from-other-t/146476/3 "2008-08-21T16:52:39Z")

</div>

> The reason for this is the render :action =\> 'new business. You see,  
> when you create a part, there are 2 ways to go; either you are  
> succesfull or not. If you are then everything is groovy and you  
> redirect\_to wherever, the redirect\_to, disposes of all your variables  
> (@part, @vendors…) and calls the new action method as if it was the  
> first time. If its not succesfull however, we cant redirect\_to because  
> wed loose all the data that the user put in in the first place and  
> restart with a blank form. So we render, which means, grab the view,  
> and render it as if i was that method but do not call the method. that  
> is why you need to initialize the variables again, thats why ive put  
> them in the else statement, so that they only get loaded if you are  
> not succesfull.
> 
> try this, remove the “duplicate” varianble assignments and create a  
> part that is succesfull, it wont complain.

Thanks “Wolas!”

It does help in this particular case. But I was also wondering in  
general what’s the best way or any other way to do? Globals?

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/is-there-a-more-efficient-way-to-work-dropdowns-from-other-t/146476/4 "2008-08-21T16:57:50Z")

</div>

A better example might be with these 2 tables:

class CreatePurchaseOrders \< ActiveRecord::Migration  
def self.up  
create\_table :purchase\_orders do |t|  
t.string :status\_id  
t.integer :submitted\_by  
t.integer :ordered\_by  
t.integer :approved\_by  
t.text :comments  
t.integer :vendor\_id  
t.string :vendor\_specific\_po\_id

```
  t.timestamps
end

```

end

def self.down  
drop\_table :purchase\_orders  
end  
end

orders\_by, submitted\_by, and approved\_by - all refer to Users table -  
and potenttially one person, or multiple so how do I setup association  
in this case?

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/is-there-a-more-efficient-way-to-work-dropdowns-from-other-t/146476/5 "2008-08-21T17:27:01Z")

</div>

On 21 aug, 16:57, Kon U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> t.integer :vendor_id
> t.string :vendor_specific_po_id
> 
> t.timestamps
> end
> 
> ```
> 
> end
> 
> def self.down  
> drop\_table :purchase\_orders  
> end  
> end

> orders\_by, submitted\_by, and approved\_by - all refer to Users table -  
> and potenttially one person, or multiple so how do I setup association  
> in this case?

You can add many belongs\_to clauses with different names all referring  
to the same class:

class PurchaseOrder \< AR::Base  
belongs\_to :submitter, :class\_name =\> “User”, :foreign\_key  
=\> :submitted\_by  
belongs\_to :orderer, :class\_name =\> “User”, :foreign\_key  
=\> :ordered\_by  
belongs\_to :approver, :class\_name =\> “User”, :foreign\_key  
=\> :approved\_by  
end

and then it’s possible to make the following queries on a  
@purchase\_order instance:  
@purchase\_order.submitter # =\> the user that is referenced by  
submitted\_by.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/is-there-a-more-efficient-way-to-work-dropdowns-from-other-t/146476/6 "2008-08-21T17:40:29Z")

</div>

On 21 Aug 2008, at 15:57, Kon U. wrote:

> t.integer :approved\_by  
> drop\_table :purchase\_orders  
> end  
> end
> 
> orders\_by, submitted\_by, and approved\_by - all refer to Users table -  
> and potenttially one person, or multiple so how do I setup  
> association  
> in this case?

> **[Creating multiple associations with the same table - Space Vatican](https://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table/)**
>
> Rails makes setting up your associations dead easy and very concise. There’s a whole bunch of options that can be set however as long as you …

The convention in rails would be for submitted\_by etc… to be  
submitted\_by\_id and for the association to be submitted\_by (weird shit  
happens if an association and a column have exactly the same name)  
Fred
