Erol, that's so cool. I like your solution a lot.

What about this one:

I allow users to filter tickets based on the module (I call them
components) they report on. The list of components comes in
as an array. Here's what I do.

# Get a list of components
@components = Component.find :all

# This is the hash that will be used in a SQL statement. It will
become clear in a second.
@filter_components = {}

# Initialize all components to true (this is the default -- we show
tickets for all components)
@components.each { |component| @filter_components[component.id] =
true }

if request.xhr?
  @components.each do |component|
    # Shouldn't happen, but if so, empty @filter_components. This
means no tickets will be selected.
    if params[:filter_components].empty?
      @filter_components = {}
      break;
    end

    if (params[:filter_components]).include?(component.id.to_s)
      @filter_components[component.id] = true
    else
      @filter_components[component.id] = false
    end
  end

  # Make sure at least one component is in @filter_components so that
we don't have special cases.
  @filter_components[0] = true if @filter_components.size == 0
end

# Now comes the line about which I was asking in my previous email.
The result of it is used in an IN() part of a SQL query
components = @filter_components.reject{ |key,value| !
value}.keys.join(',')

It works but it's a lot of code and the problem is I need to do the
same for
statuses! Any way to reduce this and make it reusable for statuses?
BTW, the
reason why I'm not using params[:filter_components] directly in the
SQL query
(e.g. params[:filter_components].join(',')) is so that the data from
the web is
not used in a SQL query directly. Make sense?

Thanks!!!

P.S.: I typed this twice! The first version of this was lost. Bummer.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to