Forum: Ruby on Rails Dynamic finder conditions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (Guest)
on 2006-04-13 11:27
(Received via mailing list)
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
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-04-13 23:11
(Received via mailing list)
Hi-

On Apr 13, 2006, at 2:26 AM, Daniel Higginbotham wrote:

> 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!
>
> Daniel

	I 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'
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (Guest)
on 2006-04-13 23:32
(Received via mailing list)
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!
This topic is locked and can not be replied to.