[For those of you reading along, I broke down and implemented a time logger
so I can figure out who I¹m working for and how much]
I¹m used to the bad old PHP way of building SQL query strings to add search
criteria. What I want to do is filter a list optionally by:
Client
Project
Date
I can write the SQL for this, but wonder... is there some magic in Rails
that might help add criteria? For example, the user wants to see all time
spent on ³project x² for ³abc, inc² and I¹m left writing WHERE clauses
additively. E.g.,
def add_filter(name, op, param)
@query[0] += '' AND '' unless @query[0].empty?
@query[0] += "#{name}#{op}?"
@query.push param
End
Then calling it as...
when ''client_filter''
add_filter ''client_id'', ''='',
params[:client_id]
Is there some magic I¹m missing?
Thanks
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Hi, 2005/10/20, Steve Ross <sross@calicowebdesigns.com>:> [For those of you reading along, I broke down and implemented a time logger > so I can figure out who I'm working for and how much] > > I'm used to the bad old PHP way of building SQL query strings to add search > criteria. What I want to do is filter a list optionally by: > > Client > Project > Date > > I can write the SQL for this, but wonder... is there some magic in Rails > that might help add criteria? For example, the user wants to see all time > spent on "project x" for "abc, inc" and I'm left writing WHERE clauses > additively. E.g., > > def add_filter(name, op, param) > @query[0] += ' AND ' unless @query[0].empty? > @query[0] += "#{name}#{op}?" > @query.push param > EndLooks good, but I use another idiom instead: conditions = [] values = [] case params[:type] when 'project' conditions << 'project like ?' values << "%#{params[:project]}%" when 'name' conditions << 'name like ?' values << "%#{params[:name]}%" end @results = SomeClass.find(:all, :conditions => [conditions.join(' AND '), *values]) In my mind, I feel this is clearer. Also, if you add multiple ? to conditions, don't forget to add as many values, or you'll get an exception. Hope that helps ! François _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
You might want to revisit how you are approaching this ...
The Rails way would be to create ActiveRecord models for Client and
Project and establish the relationship between those ... I would guess
that a Project :belongs_to a Client, and a Client :has_many Projects:
eg:
Class Client < ActiveRecord::Base
has_many :projects
end
Class Project < ActiveRecord::Base
belongs_to :client
end
Then your client_filter comes from the model, eg
client_projects = Client.find(client_id).projects
________________________________
From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Steve Ross
Sent: Friday, 21 October 2005 1:12 PM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails] Creating SQL Filters
[For those of you reading along, I broke down and implemented a time
logger so I can figure out who I''m working for and how much]
I''m used to the bad old PHP way of building SQL query strings to add
search criteria. What I want to do is filter a list optionally by:
Client
Project
Date
I can write the SQL for this, but wonder... is there some magic in Rails
that might help add criteria? For example, the user wants to see all
time spent on "project x" for "abc, inc" and I''m
left writing WHERE
clauses additively. E.g.,
def add_filter(name, op, param)
@query[0] += '' AND '' unless @query[0].empty?
@query[0] += "#{name}#{op}?"
@query.push param
End
Then calling it as...
when ''client_filter''
add_filter ''client_id'', ''='',
params[:client_id]
Is there some magic I''m missing?
Thanks
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
It¹s actually more like what you describe: class task belongs_to :client belongs_to :project class time_record belongs_to :task The deal is that I am filtering on client, project, and date-range in time-record. On 10/20/05 10:03 PM, "Neville Burnell" <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote:> You might want to revisit how you are approaching this ... > > The Rails way would be to create ActiveRecord models for Client and Project > and establish the relationship between those ... I would guess that a Project > :belongs_to a Client, and a Client :has_many Projects: > > eg: > > Class Client < ActiveRecord::Base > has_many :projects > end > > Class Project < ActiveRecord::Base > belongs_to :client > end > > Then your client_filter comes from the model, eg > > client_projects = Client.find(client_id).projects > > > > > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Steve Ross > Sent: Friday, 21 October 2005 1:12 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] Creating SQL Filters > > [For those of you reading along, I broke down and implemented a time logger so > I can figure out who I¹m working for and how much] > > I¹m used to the bad old PHP way of building SQL query strings to add search > criteria. What I want to do is filter a list optionally by: > > Client > Project > Date > > I can write the SQL for this, but wonder... is there some magic in Rails that > might help add criteria? For example, the user wants to see all time spent on > ³project x² for ³abc, inc² and I¹m left writing WHERE clauses additively. > E.g., > > def add_filter(name, op, param) > @query[0] += '' AND '' unless @query[0].empty? > @query[0] += "#{name}#{op}?" > @query.push param > End > > Then calling it as... > > when ''client_filter'' > add_filter ''client_id'', ''='', params[:client_id] > > Is there some magic I¹m missing? > > Thanks_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails