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!
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 J.
On Jul 11, 4:05 pm, kevin lee [email protected]
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 J. 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 J.
On Jul 11, 4:05�pm, kevin lee [email protected]
On Sun, Jul 26, 2009 at 7:02 PM, kevin
lee[email protected] 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 S. ------------------------ [email protected]
twitter: @hassan
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 S. wrote:
On Sun, Jul 26, 2009 at 7:02 PM, kevin
lee[email protected] 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 S. ------------------------ [email protected]
twitter: @hassan
On Wed, Jul 29, 2009 at 10:40 AM, kevin
lee[email protected] 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 S. ------------------------ [email protected]
twitter: @hassan
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 S. wrote:
On Wed, Jul 29, 2009 at 10:40 AM, kevin
lee[email protected] 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 S. ------------------------ [email protected]
twitter: @hassan
On Wed, Aug 12, 2009 at 12:24 PM, kevin
lee[email protected] 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 S. ------------------------ [email protected]
twitter: @hassan
With your guidance, I have now seen named_scope work in my simple app by
invoking it in an action! Thanks!
Hassan S. wrote:
On Wed, Aug 12, 2009 at 12:24 PM, kevin
lee[email protected] 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 S. ------------------------ [email protected]
twitter: @hassan