Hi,
I''m looking to optionally accept some conditions to my
controller#list function (which is modified from the scaffold) via
parameters so that I can offer a (very!) basic search interface. So
far I''ve got something along the lines of:
def list
conditions = [''1'']
if params[:first_name]
conditions[0] += ['' AND first_name = ?'']
conditions.push[params[:first_name]]
end
if params[:last_name]
conditions[0] += ['' AND last_name = ?'']
conditions.push[params[:last_name]]
end
@guest_pages, @guests = paginate :guest, :per_page => 10, :order
=> "last_name", :conditions => conditions
end
But I''m sure there''s got to be a more Railsish way of doing
it? Any
thoughts?
Cheers,
Graeme
--
Mail: mathie-AqrfM6TJKtazQB+pC5nmwQ@public.gmane.org | Web: http://woss.name/
AIM: Math1e | PGP: 1024D/D72F2737
Hi Graeme,
I''ve been looking at a similar issue. There is search_generator:
http://wiki.rubyonrails.com/rails/pages/SearchGenerator
Although this may be too complex for your needs.
My own simple hack for basic search is pretty much the same as yours:
def search
conditions = "1"
params[:search_terms].each {|value| conditions+= " OR description
LIKE ''%"+value+"%''" }
@discussion_pages, @discussions = paginate :discussion, :per_page =>
5, :conditions => conditions
end
not much of a difference perhaps ... I would also be interested in other
effective ways of coding this. I guess something we could do is turn
something like this into a search_helper ...? Although helpers are for
templates - I wonder if there is an equivalent for controllers?
I have however created a version of highlight, called highlight_all,
slapped it in my application_helper.rb and can now highlight all the
search terms in the results:
def highlight_all(text, phrases, highlighter = ''<strong
class="highlight">\1</strong>'')
phrases.each { |phrase|
unless phrase.blank?
text = text.gsub(/(#{Regexp.escape(phrase)})/i, highlighter)
unless text.nil?
end
}
return text
end
e.g.
<%= highlight_all(discussion.description,params[:search_terms])
%><br>
CHEERS> SAM
Graeme Mathieson wrote:
> Hi,
>
> I''m looking to optionally accept some conditions to my
> controller#list function (which is modified from the scaffold) via
> parameters so that I can offer a (very!) basic search interface. So
> far I''ve got something along the lines of:
>
> def list
> conditions = [''1'']
> if params[:first_name]
> conditions[0] += ['' AND first_name = ?'']
> conditions.push[params[:first_name]]
> end
> if params[:last_name]
> conditions[0] += ['' AND last_name = ?'']
> conditions.push[params[:last_name]]
> end
> @guest_pages, @guests = paginate :guest, :per_page => 10, :order
> => "last_name", :conditions => conditions
> end
>
> But I''m sure there''s got to be a more Railsish way of
doing it? Any
> thoughts?
>
> Cheers,
>
> Graeme
On Nov 7, 2005, at 3:14 AM, Graeme Mathieson wrote:> Hi, > > I''m looking to optionally accept some conditions to my > controller#list function (which is modified from the scaffold) via > parameters so that I can offer a (very!) basic search interface. > So far I''ve got something along the lines of: > > def list > conditions = [''1''] > if params[:first_name] > conditions[0] += ['' AND first_name = ?''] > conditions.push[params[:first_name]] > end > if params[:last_name] > conditions[0] += ['' AND last_name = ?''] > conditions.push[params[:last_name]] > end > @guest_pages, @guests = paginate :guest, :per_page => > 10, :order => "last_name", :conditions => conditions > end > > But I''m sure there''s got to be a more Railsish way of doing it? > Any thoughts? > > Cheers, > > GraemeI got this solution from someone on the list. Sorry I can''t remember who. This will easily create on the fly where clauses for your :conditions in a quesry: class A def initialize(p,m,u) @project = p @month = m @user = u end def test q = [] ary = [] [:project, :month, :user].each {|i| iv = instance_variable_get("@#{i}") unless iv.zero? q << "#{i} = ?" ary << iv end } return [q.join(" and ")].concat(ary) end end a = A.new(1,2,3) b = A.new(0,1,2) c = A.new(1,0,2) d = A.new(0,0,1) p a.test p b.test p c.test p d.test ["project = ? and month = ? and user = ?", 1, 2, 3] ["month = ? and user = ?", 1, 2] ["project = ? and user = ?", 1, 2] ["user = ?", 1] HTH- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org