我是 Rails 的初学者,我的范围有问题。
我的班级有 2 个范围:
class Event < ActiveRecord::Base
belongs_to :continent
belongs_to :event_type
scope :continent, lambda { |continent|
return if continent.blank?
composed_scope = self.scoped
composed_scope = composed_scope.where('continent_id IN ( ? )', continent).all
return composed_scope
}
scope :event_type, lambda { |eventType|
return if eventType.blank?
composed_scope = self.scoped
composed_scope = composed_scope.where('event_type_id IN ( ? )', eventType).all
return composed_scope
}
结尾
在我的控制器中,我想同时使用这两个范围。我做了:
def filter
@event = Event.scoped
@event = @event.continent(params[:continents]) unless params[:continents].blank?
@event = @event.event_type(params[:event_type]) unless params[:event_type].blank?
respond_with(@event)
end
但我不工作,我有这个错误:
undefined method `event_type' for #<Array:0x7f11248cca80>
这是因为第一个作用域返回一个数组。
我该怎么做才能让它发挥作用?
谢谢 !