Topic: DRY across models

Hi, newbie here I need a method I can call across models, can anyone please tell me where to put it and what the syntax is

I currently have the following in multiple models

def self.databycompany(company_id, description)

      self.companyfind(company_id, description)

      rescue ActiveRecord::RecordNotFound
          self.companyfind(0) # record with id 0 is always the setup record

end
def self.companyfind(company_id, description)
      find(:all,
           :conditions => ['company_id = ?', "#{company_id}"],
         :order => description )
end

Re: DRY across models

Hi,
I've kind of got this to work by putting it in a module in lib. The only problem is that the rescue does not work.


so

#lib filter_company.rb
Module FilterCompany

def databycompany(company_id, description)

   companyfind(company_id, description)

   rescue ActiveRecord::RecordNotFound
          companyfind(0,description)
end

def companyfind(company_id, description)

     find(:all, :conditions => ['company_id = ?', "#{company_id}"], :order => description)
end
end

Re: DRY across models

Ahh, that would be because I'm not looking for a single record, is there the equivalent of NoRecordsFound or is .size == 0 the way to go ?

Re: DRY across models

Yeah I'd just do something like:

#try find the one you're looking for
@company = companyfind(company_id, description)
# permanently available company:
@company = companyfind(0,description) unless @company

ThinkRefresh - Free Rails Techniques Screencasts

Re: DRY across models

mighty thanks for that