Hi everyone, I''ve been working on some code which will take params and turn them into a WHERE clause for the :conditions option in finder methods. So far I''ve been able to do some very simple text matching. I''d like to be able to support ranges, and then go from there (in fact I''ve added code for date ranges but I''m embarrassed of it so I won''t show it here). Is there already something like this out there? Also, is there any way I can make my code more Ruby-fied? Here''s what I have so far: --application.rb private #this could probably stand to be renamed def dynamic_conditions_from_hash(some_hash, model) model = Class.new(model) some_hash.delete_if { |key, value| not model.column_names.include? key } conditions_from_hash(some_hash) end def conditions_from_hash(some_hash) condition = '''' some_hash.each_pair {|key, value| if value.empty? next end condition << "#{key} LIKE ''%#{value}%'' AND " } condition[0..-5].to_s end --some_controller.rb def list paginator_options = {:per_page => 10} conditions = dynamic_conditions_from_hash(params, Item) unless conditions.empty? paginator_options.merge!({:conditions => conditions}) end @item_pages, @items = paginate :items, paginator_options end Thanks, and I hope this helps! Daniel
Hi- On Apr 13, 2006, at 2:26 AM, Daniel Higginbotham wrote:> Hi everyone, > > I''ve been working on some code which will take params and turn them > into a > WHERE clause for the :conditions option in finder methods. So far > I''ve been > able to do some very simple text matching. I''d like to be able to > support > ranges, and then go from there (in fact I''ve added code for date > ranges but > I''m embarrassed of it so I won''t show it here). Is there already > something > like this out there? Also, is there any way I can make my code more > Ruby-fied? > > <snip> > Thanks, and I hope this helps! > > DanielI have a plugin that does dynamic where clauses and much more that you can play with. Now it has a few issues with raisl 1.1 and using :include=> that I have not had time to work out just yet but you can look in the code base to get an idea of how to do what you are after. And from what I think you are trying to do will work fine with the plugin. You can read about it and download it here: http://brainspl.at/articles/2006/01/30/i-have-been-busy Cheers- -Ezra''
This looks great, thank you! I was just thinking about how nice it would be to map Ruby operators to SQL operators. Are there by chance any helper methods to create form fields which will automatically be converted to ranges? Thanks again! -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Ezra Zygmuntowicz Sent: Thursday, April 13, 2006 11:08 AM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Dynamic finder conditions Hi- On Apr 13, 2006, at 2:26 AM, Daniel Higginbotham wrote:> Hi everyone, > > I''ve been working on some code which will take params and turn them > into a > WHERE clause for the :conditions option in finder methods. So far > I''ve been > able to do some very simple text matching. I''d like to be able to > support > ranges, and then go from there (in fact I''ve added code for date > ranges but > I''m embarrassed of it so I won''t show it here). Is there already > something > like this out there? Also, is there any way I can make my code more > Ruby-fied? > > <snip> > Thanks, and I hope this helps! > > DanielI have a plugin that does dynamic where clauses and much more that you can play with. Now it has a few issues with raisl 1.1 and using :include=> that I have not had time to work out just yet but you can look in the code base to get an idea of how to do what you are after. And from what I think you are trying to do will work fine with the plugin. You can read about it and download it here: http://brainspl.at/articles/2006/01/30/i-have-been-busy Cheers- -Ezra'' _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails