Hi all, In Agile Web Dev w/Rails it is shown how to order a collection by date as: # Return a list of products we can sell (which means they have to be # available). Show the most recently available first. def self.salable_items find(:all, :conditions => "date_available <= now()", :order => "date_available desc") end What if I do not want to be tied to MySQL (the now function)? If I want to migrate to another db I''d have to change my source code, which is not good, right? What is a better alternative? Thanks, Abdullah
If you don''t want to use the now() function, don''t -- use Time.now or something and pass it to the condition as a param. I don''t think rdbms-specific sql is bad, though. Most of the time, you have to address your particular RDBMS in order to optimize queries. Phrasebooks are great for this purpose, and could certainly be implemented in a Rails app. On Jul 24, 2005, at 11:33 AM, Abdullah Jibaly wrote:> Hi all, > > In Agile Web Dev w/Rails it is shown how to order a collection by > date as: > > # Return a list of products we can sell (which means they have to be > # available). Show the most recently available first. > def self.salable_items > find(:all, > :conditions => "date_available <= now()", > :order => "date_available desc") > end > > What if I do not want to be tied to MySQL (the now function)? If I > want to > migrate to another db I''d have to change my source code, which is > not good, > right? What is a better alternative? > > Thanks, > Abdullah > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks Toby. I didn''t get what you mean by ''phrasebooks'', could you explain? Thanks, Abdullah --- Toby Boudreaux <rails-lb8SQxIZKShBDgjK7y7TUQ@public.gmane.org> wrote:> If you don''t want to use the now() function, don''t -- use Time.now or > something and pass it to the condition as a param. > > I don''t think rdbms-specific sql is bad, though. Most of the time, > you have to address your particular RDBMS in order to optimize > queries. Phrasebooks are great for this purpose, and could certainly > be implemented in a Rails app. > > > > On Jul 24, 2005, at 11:33 AM, Abdullah Jibaly wrote: > > > Hi all, > > > > In Agile Web Dev w/Rails it is shown how to order a collection by > > date as: > > > > # Return a list of products we can sell (which means they have to be > > # available). Show the most recently available first. > > def self.salable_items > > find(:all, > > :conditions => "date_available <= now()", > > :order => "date_available desc") > > end > > > > What if I do not want to be tied to MySQL (the now function)? If I > > want to > > migrate to another db I''d have to change my source code, which is > > not good, > > right? What is a better alternative? > > > > Thanks, > > Abdullah > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
By the way, could you also show how I can change the call below to use Time.now? Thanks! Abdullah --- Toby Boudreaux <rails-lb8SQxIZKShBDgjK7y7TUQ@public.gmane.org> wrote:> If you don''t want to use the now() function, don''t -- use Time.now or > something and pass it to the condition as a param. > > I don''t think rdbms-specific sql is bad, though. Most of the time, > you have to address your particular RDBMS in order to optimize > queries. Phrasebooks are great for this purpose, and could certainly > be implemented in a Rails app. > > > > On Jul 24, 2005, at 11:33 AM, Abdullah Jibaly wrote: > > > Hi all, > > > > In Agile Web Dev w/Rails it is shown how to order a collection by > > date as: > > > > # Return a list of products we can sell (which means they have to be > > # available). Show the most recently available first. > > def self.salable_items > > find(:all, > > :conditions => "date_available <= now()", > > :order => "date_available desc") > > end > > > > What if I do not want to be tied to MySQL (the now function)? If I > > want to > > migrate to another db I''d have to change my source code, which is > > not good, > > right? What is a better alternative? > > > > Thanks, > > Abdullah > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
The Phrasebook design pattern allows you to keep your SQL statements in a (single, usually) location. Instead of ever having SQL hard- coded in your model layer, you ask the phrasebook for the SQL by key. You might do something like: :conditions => [Phrasebook.get("salable_items_order_by"), Time.now], Or def self.salable_items find_by_sql(Phrasebook.get("salable_items")) end Keep in mind that Time.now won''t necessarily get the time on your particular DB box or boxes. That could be bad... def self.salable_items find(:all, :conditions => ["date_available <= ?", Time.now], :order => "date_available desc") end On Jul 24, 2005, at 11:46 AM, Abdullah Jibaly wrote:>>> def self.salable_items >>> find(:all, >>> :conditions => "date_available <= now()", >>> :order => "date_available desc") >>> end_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
This looks really interesting, in fact, why can this be used for i18n? I''ve been trying to figure a an easy way to do that in Rails and this looks like what I need! Is Phrasebook something already a part of rails? Since you used the term design pattern I am assuming it is not... Thanks! Abdullah --- Toby Boudreaux <rails-lb8SQxIZKShBDgjK7y7TUQ@public.gmane.org> wrote:> The Phrasebook design pattern allows you to keep your SQL statements > in a (single, usually) location. Instead of ever having SQL hard- > coded in your model layer, you ask the phrasebook for the SQL by key. > > You might do something like: > > :conditions => [Phrasebook.get("salable_items_order_by"), > Time.now], > > Or > > def self.salable_items > find_by_sql(Phrasebook.get("salable_items")) > end > > > > Keep in mind that Time.now won''t necessarily get the time on your > particular DB box or boxes. That could be bad... > > def self.salable_items > find(:all, > :conditions => ["date_available <= ?", Time.now], > :order => "date_available desc") > end > > > > > On Jul 24, 2005, at 11:46 AM, Abdullah Jibaly wrote: > > >>> def self.salable_items > >>> find(:all, > >>> :conditions => "date_available <= now()", > >>> :order => "date_available desc") > >>> end > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
It''s a pattern - a way of addressing and solving an problem - so it''s available anywhere, but you have to write it ;) It can be used for i18n and is in many languages (properties/messages files in Java, for instance). On Jul 24, 2005, at 11:17 PM, Abdullah Jibaly wrote:> This looks really interesting, in fact, why can this be used for > i18n? I''ve > been trying to figure a an easy way to do that in Rails and this > looks like > what I need! Is Phrasebook something already a part of rails? Since > you used > the term design pattern I am assuming it is not... > > Thanks! > Abdullah > > --- Toby Boudreaux <rails-lb8SQxIZKShBDgjK7y7TUQ@public.gmane.org> wrote: > > >> The Phrasebook design pattern allows you to keep your SQL statements >> in a (single, usually) location. Instead of ever having SQL hard- >> coded in your model layer, you ask the phrasebook for the SQL by key. >> >> You might do something like: >> >> :conditions => [Phrasebook.get("salable_items_order_by"), >> Time.now], >> >> Or >> >> def self.salable_items >> find_by_sql(Phrasebook.get("salable_items")) >> end >> >> >> >> Keep in mind that Time.now won''t necessarily get the time on your >> particular DB box or boxes. That could be bad... >> >> def self.salable_items >> find(:all, >> :conditions => ["date_available <= ?", Time.now], >> :order => "date_available desc") >> end >> >> >> >> >> On Jul 24, 2005, at 11:46 AM, Abdullah Jibaly wrote: >> >> >>>>> def self.salable_items >>>>> find(:all, >>>>> :conditions => "date_available <= now()", >>>>> :order => "date_available desc") >>>>> end >>>>> >> >> >>> _______________________________________________ >>> >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >