Re: Ruby elegance

dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
Mon, 25 Jun 2007 11:13:47 -0000

Hi --

On Mon, 25 Jun 2007, Michael wrote:

>
> Hello,
> I realise that Ruby is a both powerful and elegant language, so I have a
> question on how to improve my code. I am currently working on a CMS. I
> decided to put some predefined and constant database rows into a table,
> and that code must be run once at the initialization of the application
> (in environment.rb). So below is that code:
>
> RawTexts = 6
> if RawText.find(:all).length != RawTexts do

See Jeremy''s answer for shortening the code.  Also you can do:

   if RawText.count

instead of find(:all).length.  That will save you loading all of the
records into memory.


David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)