kastberg@tkwsping.nl
2006-Aug-19 00:37 UTC
[Rails] Special ruby language for describing sql conditions
I was brainstorming today about a smooth way to define conditions in an sql query, when the numbers of attributes increase, so does the uglyness. So instead of passing a hash, I thought you could specify the conditions directly in code. I hacked together some example code which actually turned out to work. The result is concise and pretty beutiful. def search(params) Ad.find(:all) do |conditions| conditions.area_id = params[:area_id] if params[:area_id] && params[:area_id].to_i > 0 conditions.kind = params[:kind] if params[:kind] conditions.selling = params[:selling] || 1 conditions.approved > 1 conditions.year.include?(2004,2005) conditions.or do |conditions| if params[:category_id] && params[:category_id] > 0 conditions.category_id = params[:category_id] conditions.parent_id = params[:category_id] end end end end p(search( :area_id => 0, :kind => ''f'', :category_id => 13)) # => "ads.kind = ''f'' AND ads.selling = ''1'' AND ads.approved > 1 AND ads.year IN (2004,2005) AND (ads.category_id = ''13'' OR ads.parent_id ''13'')" I''m wondering if anyone is interested since I like it very much, and would be happy to see this in rails.
Hammed Malik
2006-Aug-19 00:42 UTC
[Rails] Special ruby language for describing sql conditions
Something like this is available as a plugin and works really well - Thanks Ezra! ez-where: http://brainspl.at/articles/2006/06/30/new-release-of-ez_where-plugin Hammed On 8/19/06, kastberg@tkwsping.nl <kastberg@tkwsping.nl> wrote:> > I was brainstorming today about a smooth way to define conditions in an > sql query, when the numbers of attributes increase, so does the uglyness. > So instead of passing a hash, I thought you could specify the conditions > directly in code. > I hacked together some example code which actually turned out to work. > The result is concise and pretty beutiful. > > def search(params) > Ad.find(:all) do |conditions| > conditions.area_id = params[:area_id] if params[:area_id] && > params[:area_id].to_i > 0 > conditions.kind = params[:kind] if params[:kind] > conditions.selling = params[:selling] || 1 > conditions.approved > 1 > conditions.year.include?(2004,2005) > conditions.or do |conditions| > if params[:category_id] && params[:category_id] > 0 > conditions.category_id = params[:category_id] > conditions.parent_id = params[:category_id] > end > end > end > end > p(search( :area_id => 0, :kind => ''f'', :category_id => 13)) > # => "ads.kind = ''f'' AND ads.selling = ''1'' AND ads.approved > 1 AND > ads.year IN (2004,2005) AND (ads.category_id = ''13'' OR ads.parent_id > ''13'')" > > I''m wondering if anyone is interested since I like it very much, and would > be happy to see this in rails. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060819/05c82004/attachment.html
kastberg@tkwsping.nl
2006-Aug-19 01:01 UTC
[Rails] Special ruby language for describing sql conditions
Ah, thanks, there are so many plugins it''s impossible to keep track of them all.> Something like this is available as a plugin and works really well - > Thanks > Ezra! > > ez-where: > http://brainspl.at/articles/2006/06/30/new-release-of-ez_where-plugin > > Hammed > > On 8/19/06, kastberg@tkwsping.nl <kastberg@tkwsping.nl> wrote: >> >> I was brainstorming today about a smooth way to define conditions in an >> sql query, when the numbers of attributes increase, so does the >> uglyness. >> So instead of passing a hash, I thought you could specify the conditions >> directly in code. >> I hacked together some example code which actually turned out to work. >> The result is concise and pretty beutiful. >> >> def search(params) >> Ad.find(:all) do |conditions| >> conditions.area_id = params[:area_id] if params[:area_id] && >> params[:area_id].to_i > 0 >> conditions.kind = params[:kind] if params[:kind] >> conditions.selling = params[:selling] || 1 >> conditions.approved > 1 >> conditions.year.include?(2004,2005) >> conditions.or do |conditions| >> if params[:category_id] && params[:category_id] > 0 >> conditions.category_id = params[:category_id] >> conditions.parent_id = params[:category_id] >> end >> end >> end >> end >> p(search( :area_id => 0, :kind => ''f'', :category_id => 13)) >> # => "ads.kind = ''f'' AND ads.selling = ''1'' AND ads.approved > 1 AND >> ads.year IN (2004,2005) AND (ads.category_id = ''13'' OR ads.parent_id >> ''13'')" >> >> I''m wondering if anyone is interested since I like it very much, and >> would >> be happy to see this in rails. >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >
Hammed Malik
2006-Aug-19 01:04 UTC
[Rails] Special ruby language for describing sql conditions
http://agilewebdevelopment.com/plugins stays pretty up to date with plugin releases. On 8/19/06, kastberg@tkwsping.nl <kastberg@tkwsping.nl> wrote:> > Ah, thanks, there are so many plugins it''s impossible to keep track of > them all. > > > Something like this is available as a plugin and works really well - > > Thanks > > Ezra! > > > > ez-where: > > http://brainspl.at/articles/2006/06/30/new-release-of-ez_where-plugin > > > > Hammed > > > > On 8/19/06, kastberg@tkwsping.nl <kastberg@tkwsping.nl> wrote: > >> > >> I was brainstorming today about a smooth way to define conditions in an > >> sql query, when the numbers of attributes increase, so does the > >> uglyness. > >> So instead of passing a hash, I thought you could specify the > conditions > >> directly in code. > >> I hacked together some example code which actually turned out to work. > >> The result is concise and pretty beutiful. > >> > >> def search(params) > >> Ad.find(:all) do |conditions| > >> conditions.area_id = params[:area_id] if params[:area_id] && > >> params[:area_id].to_i > 0 > >> conditions.kind = params[:kind] if params[:kind] > >> conditions.selling = params[:selling] || 1 > >> conditions.approved > 1 > >> conditions.year.include?(2004,2005) > >> conditions.or do |conditions| > >> if params[:category_id] && params[:category_id] > 0 > >> conditions.category_id = params[:category_id] > >> conditions.parent_id = params[:category_id] > >> end > >> end > >> end > >> end > >> p(search( :area_id => 0, :kind => ''f'', :category_id => 13)) > >> # => "ads.kind = ''f'' AND ads.selling = ''1'' AND ads.approved > 1 AND > >> ads.year IN (2004,2005) AND (ads.category_id = ''13'' OR ads.parent_id > >> ''13'')" > >> > >> I''m wondering if anyone is interested since I like it very much, and > >> would > >> be happy to see this in rails. > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060819/137dbe77/attachment.html