Beating a dead horse - proper association selection

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

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