Hi, I am trying to implement a few queries now. What are good ways to do this? Right now, I have a list page that does sorting and can carry out a generic pagination request. @user_result_pages, @user_results = paginate(:user_results, :per_page => 20, :conditions => @condition, :order_by => @sort_order) I create @sort_order earlier in the list function. The problem I am having is with ''@condition'' I''m not sure what''s the best way to create this. There are a number of options that my user may select: 1. date > ... 2. range of dates 3. specific dates 4. name is LIKE ... and so on.. I''m therefore having trouble conceptualizing how to create this condition string. All the examples I have seen so far talk about it as :conditions => [?created_on > ??, time] and so on. I can''t find an example that helps me formulate this string earlier. Another example I saw uses ''sanitize'' which seems to be available in the model but not the controller, so I can''t seem to use that either. Help would be greatly appreciated :D Thanks Mohit.
A good way to start is to remember that :conditions is not a String but an Array. The first index of the :conditions is the sqlish query which can be built anyway you see fit and the arguments can then be added (+) to it to create the final :conditions Array. :conditions => [query string with ?] + [arugment1] Mohit Sindhwani wrote:> Hi, I am trying to implement a few queries now. What are good ways to > do this? > > Right now, I have a list page that does sorting and can carry out a > generic pagination request. > @user_result_pages, @user_results = paginate(:user_results, > :per_page => 20, > :conditions => @condition, > :order_by => @sort_order) > > I create @sort_order earlier in the list function. The problem I am > having is with ''@condition'' I''m not sure what''s the best way to create > this. There are a number of options that my user may select: > 1. date > ... > 2. range of dates > 3. specific dates > 4. name is LIKE ... > and so on.. > > I''m therefore having trouble conceptualizing how to create this > condition string. All the examples I have seen so far talk about it as > :conditions => [?created_on > ??, time] and so on. I can''t find an > example that helps me formulate this string earlier. > > Another example I saw uses ''sanitize'' which seems to be available in > the model but not the controller, so I can''t seem to use that either. > > Help would be greatly appreciated :D > Thanks > Mohit. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060817/3340ca40/attachment.html
Thanks, Todd! That helped me to get started :) Cheers Mohit. Todd Makinster wrote:> A good way to start is to remember that :conditions is not a String > but an Array. > The first index of the :conditions is the sqlish query which can be > built anyway you > see fit and the arguments can then be added (+) to it to create the > final :conditions > Array. :conditions => [query string with ?] + [arugment1] > > > > Mohit Sindhwani wrote: >> Hi, I am trying to implement a few queries now. What are good ways to >> do this? >> >> Right now, I have a list page that does sorting and can carry out a >> generic pagination request. >> @user_result_pages, @user_results = paginate(:user_results, >> :per_page => 20, >> :conditions => @condition, >> :order_by => @sort_order) >> >> I create @sort_order earlier in the list function. The problem I am >> having is with ''@condition'' I''m not sure what''s the best way to >> create this. There are a number of options that my user may select: >> 1. date > ... >> 2. range of dates >> 3. specific dates >> 4. name is LIKE ... >> and so on.. >> >> I''m therefore having trouble conceptualizing how to create this >> condition string. All the examples I have seen so far talk about it as >> :conditions => [?created_on > ??, time] and so on. I can''t find an >> example that helps me formulate this string earlier. >> >> Another example I saw uses ''sanitize'' which seems to be available in >> the model but not the controller, so I can''t seem to use that either. >> >> Help would be greatly appreciated :D >> Thanks >> Mohit. >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >>
Hi again! I am able to create the query string correctly now for the conditions. I''d like to use my common list function to show the records. So, what I''d like to do is call a set of different functions that create the correct search conditions based on what is entered in the forms. But, although I''m able to construct the query in a controller function called ''query1'' - unfortunately this does not work: def q2 id = params[:id] qa = [''name like ?''] sid = ''%'' + params[:name] + ''%'' @qp = [sid] qa << ''test_date >= ?'' @qp << params[:start] @condition = [qa.join('' and '')] + @qp params[:condition] = @condition redirect_to :action => ''list2'' end I''m confused whether I should use: redirect_to :action => ''list2'' or render :action => ''list2'' On the other side, list2 picks it up and uses it for the pagination (if I put the above code into list2 it works fine) @condition = params[:condition] @kx21_result_pages, @kx21_results = paginate(:kx21_results, :per_page => 20, :conditions => @condition, :order_by => @sort_order) How do I pass the conditions array as a parameter to the redirect? It does work if I ''render :action => list2'' into the function that creates the query (q2 above) - but, then I must duplicate the code that checks and creates the strings for the sort order, etc... I guess I could DRY it up? Is it a good idea to break up my queries in such a manner? Thanks Mohit.> Todd Makinster wrote: >> A good way to start is to remember that :conditions is not a String >> but an Array. >> The first index of the :conditions is the sqlish query which can be >> built anyway you >> see fit and the arguments can then be added (+) to it to create the >> final :conditions >> Array. :conditions => [query string with ?] + [arugment1] >> >> >> >> Mohit Sindhwani wrote: >>> Hi, I am trying to implement a few queries now. What are good ways >>> to do this? >>> >>> Right now, I have a list page that does sorting and can carry out a >>> generic pagination request. >>> @user_result_pages, @user_results = paginate(:user_results, >>> :per_page => 20, >>> :conditions => @condition, >>> :order_by => @sort_order) >>> >>> I create @sort_order earlier in the list function. The problem I am >>> having is with ''@condition'' I''m not sure what''s the best way to >>> create this. There are a number of options that my user may select: >>> 1. date > ... >>> 2. range of dates >>> 3. specific dates >>> 4. name is LIKE ... >>> and so on.. >>> >>> I''m therefore having trouble conceptualizing how to create this >>> condition string. All the examples I have seen so far talk about it as >>> :conditions => [?created_on > ??, time] and so on. I can''t find an >>> example that helps me formulate this string earlier. >>> >>> Another example I saw uses ''sanitize'' which seems to be available in >>> the model but not the controller, so I can''t seem to use that either. >>> >>> Help would be greatly appreciated :D >>> Thanks >>> Mohit.
If you do redirect, you lose all the instance variables in q2. I would say go directly to list, and make a function call from there: def list q2 #any other list-specific actions end that will let the controller access the q2 variables from the list action. cheers, B Gates Hi again! I am able to create the query string correctly now for the conditions. I''d like to use my common list function to show the records. So, what I''d like to do is call a set of different functions that create the correct search conditions based on what is entered in the forms. But, although I''m able to construct the query in a controller function called ''query1'' - unfortunately this does not work: def q2 id = params[:id] qa = [''name like ?''] sid = ''%'' + params[:name] + ''%'' @qp = [sid] qa << ''test_date >= ?'' @qp << params[:start] @condition = [qa.join('' and '')] + @qp params[:condition] = @condition redirect_to :action => ''list2'' end I''m confused whether I should use: redirect_to :action => ''list2'' or render :action => ''list2'' On the other side, list2 picks it up and uses it for the pagination (if I put the above code into list2 it works fine) @condition = params[:condition] @kx21_result_pages, @kx21_results paginate(:kx21_results, :per_page => 20, :conditions => @condition, :order_by => @sort_order) How do I pass the conditions array as a parameter to the redirect? It does work if I ''render :action => list2'' into the function that creates the query (q2 above) - but, then I must duplicate the code that checks and creates the strings for the sort order, etc... I guess I could DRY it up? __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
B Gates wrote:> If you do redirect, you lose all the instance > variables in q2. I would say go directly to list, and > make a function call from there: > > def list > q2 > #any other list-specific actions > end > > that will let the controller access the q2 variables > from the list action. > > cheers, > B GatesThanks! Would it be a good idea to have a hidden variable then which points out what query it is and use that in my list function to decide which condition creating function should be called? def list if Request.post? if query_requested == ''name'' condition = q2 if query_requested == ''date range'' condition = q3 ... end #conditions have been created.. #and all the rest of the stuff either for GET or for using the conditions.. end
I don''t even think you need a hidden variable. If only one value from your condition options shows up in params, maybe this: def list if Request.post? hash = {''name''=>q2,''date range''=>q3} selected_condition = params.keys & hash.keys condition = hash[selected_condition] cheers, B Gates __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
I think this is a good way to do it. One thing that came to my mind is while building your conditions you may have varying number of condition arguments. For example the name query has one argument while a date range will have two. Perhaps some trickery counting ''?'' in the conditions query could do the trick or maybe a hash with the key of the query and value being the argument count. I would also think that defining the possible queries in an array/hash could be used on the front-end and back-end with consistency and would allow you to add and remove queries in the future without much more work. Good luck! B Gates wrote:>I don''t even think you need a hidden variable. If >only one value from your condition options shows up in >params, maybe this: > >def list > if Request.post? > hash = {''name''=>q2,''date range''=>q3} > selected_condition = params.keys & hash.keys > condition = hash[selected_condition] > >cheers, >B Gates > >__________________________________________________ >Do You Yahoo!? >Tired of spam? Yahoo! Mail has the best spam protection around >http://mail.yahoo.com >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060818/b4670335/attachment.html
Thanks Todd & B Gates. Off to coding I go again. I''m sure I''ll resurface and ask more.. :) Cheers Mohit. Todd Makinster wrote:> I think this is a good way to do it. One thing that came to my mind > is while building your conditions you may have varying number of > condition arguments. For example the name query has one argument > while a date range will have two. Perhaps some trickery counting ''?'' > in the conditions query could do the trick or maybe a hash with > the key of the query and value being the argument count. > > I would also think that defining the possible queries in an array/hash > could be used on the front-end and back-end with consistency and would > allow you to add and remove queries in the future without much more > work. > > Good luck! > > B Gates wrote: >> I don''t even think you need a hidden variable. If >> only one value from your condition options shows up in >> params, maybe this: >> >> def list >> if Request.post? >> hash = {''name''=>q2,''date range''=>q3} >> selected_condition = params.keys & hash.keys >> condition = hash[selected_condition] >> >> cheers, >> B Gates >>