Could you explain why the first part would go entirely into the
Machine model?

On 2 jul, 23:56, Pat Nakajima <[email protected]> wrote:
> Off the top of my head:
>   class Machine < ActiveRecord::Base
>     # Define a list of acceptable params for filtering
>     FILTERS = [:name, :code]
>
>     # Finds records by params
>     def self.filter_by_params(params)
>       conditions = params.select { |key,val| FILTERS.include?(key.to_sym)
> and val }
>       all(:conditions => conditions)
>     end
>   end
>
>   class MachinesController < ApplicationController
>     def index
>       # Where params is { :filter => { :name => 'Pat', :code => '123' }}
>       @machines = Machine.filter_by_params(params[:filter])
>     end
>   end
>
> On Thu, Jul 2, 2009 at 5:42 PM, jhaagmans <[email protected]> wrote:
>
> > In an app I'm working on, I want to have a filter in which you can
> > specify certain attributes and click "filter" to activate an AJAX
> > request and return a list of, say, machines that match with those
> > attributes.
>
> > No I'm not sure how to build the controller.
>
> > If I would specify all the attributes, I would simply do:
>
> > def filter
> > �...@machines = Machine.find(:all, :conditions => { :attr_1 => params
> > [:attr_1], :attr_2 ... }
> > end
>
> > But how would I do this if only some of the attributes are specified
> > and the others are nil? Would I have to create @machines including all
> > machines and have Rails do the rest of the work or is there a way to
> > get find to do what I want?
--~--~---------~--~----~------------~-------~--~----~
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