Forum: Ruby on Rails beating a dead horse - proper association selection

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F12368bc9ab95a09cfbfd708a04591e6?d=identicon&s=25 Frank J. Mattia (Guest)
on 2007-05-20 03:42
okay... i have two models

 - id
 - username

 - 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

@open_orders = Order.open_orders

def self.open_orders
  find(:all, :conditions => ['is_open = ?', true], :order => "created_on

<% for order in @open_orders %>
Order Number: <%=h order.number %><br />
<% end %>

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


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

thanks in advance,
- fjm
3df67e886c0d8151475beadad0a884fd?d=identicon&s=25 Sean T Allen (Guest)
on 2007-05-20 12:26
(Received via mailing list)
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 =>

then you could do:

This topic is locked and can not be replied to.