Ruby-Rails: vendor and client companies on a single model

I’m posting this mostly for my self to remember. It’s really not hard, but worth remembering. I have a model that has two foreign key fields to the company model. I wanted to expose this using nice ActiveRecord has_one .. :as => ‘xxx’ or something but doesn’t seem to be provide what I was looking for.  I wanted model.vendor to get the company marked as the vendor; and same thing for model.client.  Ultimately I went with the obvious manual way, but I’m happy with it.

class MySecretModel < ActiveRecord::Base
  belongs_to :user
  has_many :entries, :order => :on_date
  has_many :approval_requests
  # has_one :company, :foreign_key => :vendor_company_id, :as => :vendor
  # has_one :company, :foreign_key => :client_company_id, :as => :client

  def vendor
    Company.find_by_id vendor_company_id
  end
  
  def client
    Company.find_by_id client_company_id
  end

end
 
January 4, 2010 13:04 by josh
E-mail | Permalink
blog comments powered by Disqus


about josh

another programmer blogging about his misadventures in writing code.

Contact

contact us for website & software consulting

Decide

decide on pragmatic solutions

Develop

develop your product together

Succeed

achieve your goals with our services