Like the subject says, I don''t get how date_select() helper works. I need to get the classic "From: .... To: ...." date field from a form (built with the helper), and then build the relative SQL to search for through a table... This is what I thought could work: def results @sq = @params[:entry][:body] @d1 = @params[:entry][:created_on_1] @d2 = @params[:entry][:created_on_2] dcond = "" if (!@d1.nil? and !@d2.nil?) dcond = "created_on between ''#{@d1}'' and ''#{@d2}'' and " end @entries = Entry.find(:all, :limit=> 25, :conditions => [dcond + "account_id=? and body like ''#{@sq}%''", @session[:account].id ], :order => "created_on desc") render_action :index end But it didn''t. @d1 and @d2 are empty. Inspecting @params: { "created_on_1(1i)"=>"2001", "created_on_1(2i)"=>"2", "created_on_1(3i)"=>"10", "created_on_2(1i)"=>"2004", "created_on_2(2i)"=>"4", "created_on_2(3i)"=>"7", "body"=>"a" } I''m quite sure there''s some "magic" here somewhere, but I can''t get it :) So, the question is: how is the simplest way to build date range selects and then feed it to my clever mysql ? (Well, I''d obviously came up to build it from scratch... but...) -- Claudio Cicali http://www.flexer.it GPG Keyid: 1024D/555D25CE ''Nos patriae finis et dulcia linquimus arva''
> Inspecting @params: > > { > "created_on_1(1i)"=>"2001", > "created_on_1(2i)"=>"2", > "created_on_1(3i)"=>"10", > "created_on_2(1i)"=>"2004", > "created_on_2(2i)"=>"4", > "created_on_2(3i)"=>"7", > "body"=>"a" > } > > I''m quite sure there''s some "magic" here somewhere, but I can''t get it :)Thats because the magic is in ActiveRecord::Base#attributes= The date helpers can be used for assigning to models, but not for working directly with them in the controller. This seems broken to me, but it''s unlikely that we''ll fix it before 1.0 -- Cheers Koz
Michael Koziarski wrote:>>Inspecting @params: >> >>{ >> "created_on_1(1i)"=>"2001", >> "created_on_1(2i)"=>"2", >> "created_on_1(3i)"=>"10", >> "created_on_2(1i)"=>"2004", >> "created_on_2(2i)"=>"4", >> "created_on_2(3i)"=>"7", >> "body"=>"a" >>} >> >>I''m quite sure there''s some "magic" here somewhere, but I can''t get it :) >> >> > >Thats because the magic is in ActiveRecord::Base#attributes= The >date helpers can be used for assigning to models, but not for working >directly with them in the controller. > >This seems broken to me, but it''s unlikely that we''ll fix it before 1.0 > >how is this broken? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> how is this broken?Broken is perhaps too strong. But at first glance users clearly expect @params[:the_date] to contain the date, it should be possible to at least have a helper accessible from the controllers. The catch is that the ''magic conversion'' needs to know the class of the object to which it is assigning, this precludes us from just transparently converting the params in the request chain. -- Cheers Koz
Michael Koziarski wrote:>>how is this broken? >> >> > >Broken is perhaps too strong. > >But at first glance users clearly expect @params[:the_date] to contain >the date, it should be possible to at least have a helper accessible >from the controllers. The catch is that the ''magic conversion'' needs >to know the class of the object to which it is assigning, this >precludes us from just transparently converting the params in the >request chain. > >I suppose someone could expect it to be accessible as @params[:the_date], personally that would throw me for quite a loop... but a helper... that would be nice.. one for time, one for date what do you think good names for that those two methods would be? its a quicky job... in the date helper which i''ve already submitted a couple patches for so i could knock that off pretty quick ( and well i only have time right now for pretty quick things... sigh ) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sean T Allen wrote:>> > I suppose someone could expect it to be accessible as @params[:the_date], > personally that would throw me for quite a loop... > > but a helper... that would be nice.. > > one for time, one for date > > what do you think good names for that those two methods would be? > > its a quicky job... in the date helper which i''ve already submitted a > couple patches for so > i could knock that off pretty quick ( and well i only have time right > now for pretty quick things... sigh )Oh, ehm... mmmh... while you are there, it could be nice if the datetime_select could understand the ":order" option as the date_select() does :) ;) -- Claudio Cicali http://www.flexer.it GPG Keyid: 1024D/555D25CE ''Nos patriae finis et dulcia linquimus arva''
On Friday 01 July 2005 01:27, Michael Koziarski wrote:> But at first glance users clearly expect @params[:the_date] to > contain the date, it should be possible to at least have a helper > accessible from the controllers. The catch is that the ''magic > conversion'' needs to know the class of the object to which it is > assigning, this precludes us from just transparently converting the > params in the request chain.I have the complementary problem due to using a calendar widget on the client and also allowing free form date input. Thus, in the request I get dates as a single, possibly ill-formed, parameter. As it''s impossible to tell automatically which parameters are meant to be dates, it has to be declared explicitly: class TasksController < ApplicationController set_date_param_paths ''task.due_date'' In a controller up the inheritance hierarchy a filter is installed: before_filter Proc.new { |ctrl| itemize_dates(ctrl) }, :only => [:create, :update] The necessary methods are mixed-in from somewhere else def set_date_param_paths(*paths) cattr_accessor :date_param_paths private_class_method :date_param_paths self.date_param_paths = paths end def itemize_dates(controller) return unless controller.class.respond_to?(:date_param_paths) date_param_paths = controller.class.date_param_paths return if date_param_paths.blank? params = controller.params date_format = controller.date_format date_param_paths.each do |p| date = params.get_at_path(p, :default => nil) if date begin date = Date.strptime(date, date_format) params.set_at_path(p + ''(1i)'', date.year.to_s) params.set_at_path(p + ''(2i)'', date.month.to_s) params.set_at_path(p + ''(3i)'', date.day.to_s) rescue end end end end get_at_path is a mixed-in method that traverses a path along method calls or hash keys. So far, itemize_date is obviously in the wrong direction. But a complementary method, say coalesce_dates, could be implemented just as easily as the names of the individual date item parameters can be derived from the name of the synthetic date parameter. Michael -- Michael Schuerig This is not a false alarm mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org This is not a test http://www.schuerig.de/michael/ --Rush, Red Tide
> > Oh, ehm... mmmh... while you are there, it could be nice if the > datetime_select could understand the ":order" option as the date_select() > does :) > > ;) >http://dev.rubyonrails.com/ticket/1427 was submitted on June 12th _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Michael Koziarski wrote:>>Inspecting @params: >> >>{ >> "created_on_1(1i)"=>"2001", >> "created_on_1(2i)"=>"2", >> "created_on_1(3i)"=>"10", >> "created_on_2(1i)"=>"2004", >> "created_on_2(2i)"=>"4", >> "created_on_2(3i)"=>"7", >> "body"=>"a" >>} >> >>I''m quite sure there''s some "magic" here somewhere, but I can''t get it :) >> >> > >Thats because the magic is in ActiveRecord::Base#attributes= The >date helpers can be used for assigning to models, but not for working >directly with them in the controller. > >This seems broken to me, but it''s unlikely that we''ll fix it before 1.0 > >A have this specific helper for solving this in my application, ugly code but working: Returns a Time object that you can use in your SQL or whatever "non Active Record" statement def getStartDate 1.upto(3) do |x| tmpStr = @params[''tournament'']["start_date(#{x}i)"] if tmpStr.to_i < 10 tmpStr = "0#{tmpStr}" end retStr += tmpStr end Time.parse retStr end // JoNtE
Jonas Montonen wrote:> > def getStartDate > 1.upto(3) do |x| > tmpStr = @params[''tournament'']["start_date(#{x}i)"] > if tmpStr.to_i < 10 > tmpStr = "0#{tmpStr}" > end > retStr += tmpStr > end > Time.parse retStr > end > > // JoNtE >I''ll adopt this one... thanks :) -- Claudio Cicali http://www.flexer.it GPG Keyid: 1024D/555D25CE ''Nos patriae finis et dulcia linquimus arva''