I have a model called “event” that has a scope named “upcoming” wich returns events that are dated in the future. In the rails console, whenever I type
Event.upcoming
It returns the subset of events successfully.
However if I type:
@events = Event.all
@events.upcoming
I get an undefined method ‘upcoming’ error. Are scopes only working on the class and not on instantiated variables?
Thanks in advance.
Yohann
Once you call
.all,@eventsis no longer anActiveRelation, hence you cannot call a scope on it.So,
Event.where(SOME CONDITIONS).order(ORDERING).upcoming.limit(X)would still work, butEvent.where(SOME CONDITIONS).order(ORDERING).all.upcoming.limit(X)would not work.