kevin lee
2009-Jul-11 20:05 UTC
Filter out or not showing event records that are in the past
My app shows a list of upcoming events. Without having to modify the database frequently to keep up with the current date, I would like to be able to keep the events that are now in the past from being shown. Can I do this using filter? Any example or link to a tutorial would be appreciated. Thanks! -- Posted via http://www.ruby-forum.com/.
Matt Jones
2009-Jul-12 17:52 UTC
Re: Filter out or not showing event records that are in the past
Try this in your event model: named_scope :upcoming, lambda { { :conditions => [''happens_at > ?'', Time.now] } } I''ve assumed that your scheduled date is named ''happens_at''; tweak as needed. --Matt Jones On Jul 11, 4:05 pm, kevin lee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> My app shows a list of upcoming events. Without having to modify the > database frequently to keep up with the current date, I would like to be > able to keep the events that are now in the past from being shown. Can > I do this using filter? Any example or link to a tutorial would be > appreciated. Thanks! > -- > Posted viahttp://www.ruby-forum.com/.
kevin lee
2009-Jul-27 02:02 UTC
Re: Filter out or not showing event records that are in the past
I tweaked the sample code for my model and called MyModel.NameofScope (i.e. Event.upcoming) in the model. To my surprise, it has no effect. I also tried one or two other conditions. I looked at the log and there is nothing related to named_scope. I use fixtures to load data into the database (SQLite3) but I don''t think it matters. My Rails version is 2.1 So how do I go about troubleshooting this problem? Any advice? Thanks! Matt Jones wrote:> Try this in your event model: > > named_scope :upcoming, lambda { { :conditions => [''happens_at > ?'', > Time.now] } } > > I''ve assumed that your scheduled date is named ''happens_at''; tweak as > needed. > > --Matt Jones > > On Jul 11, 4:05�pm, kevin lee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/.
Hassan Schroeder
2009-Jul-27 06:20 UTC
Re: Filter out or not showing event records that are in the past
On Sun, Jul 26, 2009 at 7:02 PM, kevin lee<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I tweaked the sample code for my model and called MyModel.NameofScope > (i.e. Event.upcoming) in the model. To my surprise, it has no effect. > I also tried one or two other conditions. I looked at the log and there > is nothing related to named_scope. I use fixtures to load data into the > database (SQLite3) but I don''t think it matters. My Rails version is > 2.1 So how do I go about troubleshooting this problem? Any advice?If your tests pass, perhaps they''re not doing what you think they are :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
kevin lee
2009-Jul-29 17:40 UTC
Re: Filter out or not showing event records that are in the past
I use ActiveScaffold in my project. I posted, perhaps stupidly, a question on AS help forum on whether it supports named_scope and no one replies for days now. I also could hardly find named_scope discussion on that forum. Hassan Schroeder wrote:> On Sun, Jul 26, 2009 at 7:02 PM, kevin > lee<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> I tweaked the sample code for my model and called MyModel.NameofScope >> (i.e. Event.upcoming) in the model. �To my surprise, it has no effect. >> I also tried one or two other conditions. �I looked at the log and there >> is nothing related to named_scope. �I use fixtures to load data into the >> database (SQLite3) but I don''t think it matters. �My Rails version is >> 2.1 �So how do I go about troubleshooting this problem? �Any advice? > > If your tests pass, perhaps they''re not doing what you think they are > :-) > > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan-- Posted via http://www.ruby-forum.com/.
Hassan Schroeder
2009-Jul-29 17:51 UTC
Re: Filter out or not showing event records that are in the past
On Wed, Jul 29, 2009 at 10:40 AM, kevin lee<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I use ActiveScaffold in my project. I posted, perhaps stupidly, a > question on AS help forum on whether it supports named_scopeI know zip about ActiveScaffold, but I imagine you would get more useful help if you posted your actual model code and the controller code where you''re calling your named scope. Though -- a suggestion -- trying things out in the console is often an effective way to debug a problem... FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
kevin lee
2009-Aug-12 19:24 UTC
Re: Filter out or not showing event records that are in the past
I have generated a simple event app to learn and try to debug named_scope. But still have problem. My problem is still have not figure out how to call the named_scope (i.e. Event.upcoming). Not knowing exactly how, I tried to invoked it in various spots in the controller and also the view to no avail (and also used script/console). The online articles about named_scope I found just invoke it (or listed the usage line) below the model class. It looks so simple to understand but I guess I don''t have enough brain power. Please help me out by giving a more concrete example. Thanks! Hassan Schroeder wrote:> On Wed, Jul 29, 2009 at 10:40 AM, kevin > lee<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> I use ActiveScaffold in my project. �I posted, perhaps stupidly, a >> question on AS help forum on whether it supports named_scope > > I know zip about ActiveScaffold, but I imagine you would get more > useful help if you posted your actual model code and the controller > code where you''re calling your named scope. > > Though -- a suggestion -- trying things out in the console is often an > effective way to debug a problem... > > FWIW, > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan-- Posted via http://www.ruby-forum.com/.
Hassan Schroeder
2009-Aug-12 20:33 UTC
Re: Filter out or not showing event records that are in the past
On Wed, Aug 12, 2009 at 12:24 PM, kevin lee<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have generated a simple event app to learn and try to debug > named_scope. But still have problem. My problem is still have not > figure out how to call the named_scope (i.e. Event.upcoming).Ex: named_scopes in a model: named_scope :mode, lambda { |mode| { :conditions => [''mode = ?'', mode] } } named_scope :available, lambda { |current_user| { :conditions => [ " permission = ? OR user_id = ?" , ''public'', current_user.id ] } } :: and in the controller: @notes = Note.available(current_user).mode params[:mode] :: which also demonstrates scope chaining, FWIW. HTH, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
kevin lee
2009-Aug-17 19:29 UTC
Re: Filter out or not showing event records that are in the past
With your guidance, I have now seen named_scope work in my simple app by invoking it in an action! Thanks! Hassan Schroeder wrote:> On Wed, Aug 12, 2009 at 12:24 PM, kevin > lee<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> I have generated a simple event app to learn and try to debug >> named_scope. �But still have problem. �My problem is still have not >> figure out how to call the named_scope (i.e. Event.upcoming). > > Ex: named_scopes in a model: > > named_scope :mode, lambda { |mode| { :conditions => [''mode = ?'', mode] > } } > named_scope :available, lambda { |current_user| { :conditions => [ " > permission = ? OR user_id = ?" , ''public'', current_user.id ] } } > > :: and in the controller: > > @notes = Note.available(current_user).mode params[:mode] > > :: which also demonstrates scope chaining, FWIW. > > HTH, > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan-- Posted via http://www.ruby-forum.com/.