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 %><br /> <% 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sean T Allen
2007-May-20 08:47 UTC
Re: beating a dead horse - proper association selection
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 Frank J. Mattia wrote:> 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 %><br /> > <% 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 > >