# Beating a dead horse - proper association selection

**URL:** <https://www.ruby-forum.com/t/beating-a-dead-horse-proper-association-selection/104589>\
**Category:** Rails\
**Created:** [May 20, 2007, 3:42am UTC](https://www.ruby-forum.com/t/beating-a-dead-horse-proper-association-selection/104589 "2007-05-20T03:42:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_JSMattia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@Frank\_JSMattia](https://www.ruby-forum.com/u/Frank_JSMattia)\
**Post date:** [May 20, 2007, 3:42am UTC](https://www.ruby-forum.com/t/beating-a-dead-horse-proper-association-selection/104589/1 "2007-05-20T03:42:11Z")

</div>

okay… i have two models

employee

- id
- username

order

- id
- number
- created\_by
- approved\_by

now created\_by and approved\_by are both id’s of employees. in most  
cases, two different employees.

currently i do something like

//controller  
@open\_orders = Order.open\_orders

//model  
def self.open\_orders  
find(:all, :conditions =\> [‘is\_open = ?’, true], :order =\> “created\_on  
desc”)  
end

//view  
\<% for order in @open\_orders %\>  
Order Number: \<%=h order.number %\>  
  
\<% end %\>

what type of association do i need in order to do something like

order.creator

work and have it return the more friendly ‘username’ instead of just the  
users id?

im fairly sure i want a two way has\_many but im not 100% sure of how to  
go about it.  
also, in addition to ‘orders’ there are also ‘quotes’ and ‘reviews’ that  
will rely on employees in the same way. but i’ll tackle them as i figure  
this out.

if any of that fails to make sense, bear in mind that ive only been on  
rails for 5 days now… i think i am picking it up fairly quickly  
though…

thanks in advance,

- fjm

---

<div class="post-metadata">

**Author:** ![Frank\_JSMattia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@Frank\_JSMattia](https://www.ruby-forum.com/u/Frank_JSMattia)\
**Post date:** [May 20, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/beating-a-dead-horse-proper-association-selection/104589/2 "2007-05-20T12:26:29Z")

</div>

why would it be a ‘two way has many’

an order only has 1 creator right?

class Order \< ActiveRecord::Base  
belongs\_to :creator, :class\_name =\> ‘Employee’, :foreign\_key =\>  
‘created\_by’  
…  
end

then you could do:

order.creator.username
