Paperclip and json, "stack level too deep"
I use Paperclip in one of my model :
class Event < ActiveRecord::Base
belongs_to :continent
belongs_to :event_type
scope :continent, lambda { |continent|
self.scoped.where('continent_id IN ( ? )', continent) unless continent.blank?
}
scope :event_type, lambda { |eventType|
self.scoped.where('event_type_id IN ( ? )', eventType) unless eventType.blank?
}
scope :in_date, lambda { |date|
self.scoped.where('(MONTH(`date_start`) BETWEEN ? AND ?) OR (MONTH(`date_end`) BETWEEN ? AND ?)', date[0],date[1],date[0],date[1]) unless date.blank?
}
has_attached_file :map, :styles => { :medium => "238x238>",
:thumb => "100x100>"
}
end
I make a Ajax request on this action :
def filter
@events = Event.scoped
@events = @events.continent(params[:continents]) unless params[:continents].blank?
@events = @events.event_type(params[:event_type]) 开发者_Go百科unless params[:event_type].blank?
@events = @events.in_date(params[:months]) unless params[:months].blank?
respond_with( @events )
end
I call this url to get the json answer. When i did, i get the error : "stack level too deep"
Anyone can help me?
My trace is here :
http://paste.bradleygill.com/index.php?paste_id=316663
Stack depth too deep
indicates that you ended up in an infinite loop. Your continent scope is the problem, since your method and argument have the same name, when you call the argument within the continent scope, you end up with an infinite loop.
Also, why not just write your scopes as a series of class methods? I'm not a huge fan of using lambdas to pass in arguments in scopes, as it makes it somewhat harder to read. Here is an example of having the scopes as class methods
class Event < ActiveRecord::Base
belongs_to :continent
belongs_to :event_type
class << self
def continent(cont)
where('continent_id IN ( ? )', cont) unless cont.blank?
end
def event_type(eventType)
where('event_type_id IN ( ? )', event_type_id) unless event_type_id.blank?
end
def in_date(date)
where('(MONTH(`date_start`) BETWEEN ? AND ?) OR (MONTH(`date_end`) BETWEEN ? AND ?)', date[0],date[1],date[0],date[1]) unless date.blank?
end
end
has_attached_file :map, :styles => { :medium => "238x238>",
:thumb => "100x100>"
}
end
精彩评论