简体   繁体   中英

Rails: Stack level too deep error

My rails app has 3 models. Trail, Region and Feature. I am able to interact with these models fine in my lib/tasks directory. I used anemone to crawl and populate the database. Examples of calls I made on the models:

Trail.find_or_initialize_by_title(detail_title)

I am now trying to write a controller that uses the model.

class TrailController < ApplicationController
    def index
        render :json => Trail.all
    end
end

Now if i open rails console and try app.get('trail/index') I get a 500 return code and I see the following in my development.log

SystemStackError (stack level too deep):
app/controllers/trail_controller.rb:23:in `index'

So I'm obviously causing some infinite recursion. Line 23 corresponds to the body of the index method. I've tried the other models in my app: Feature and Region and the result is the same. Can someone tell me what I'm doing wrong here, or how I can get more tracing to figure out what exactly is recursing infinitely?

My models are very simple:

class Feature < ActiveRecord::Base 
  attr_accessible :name 
  has_and_belongs_to_many :trails 
  validates :name, :presence => true
end 

class Region < ActiveRecord::Base 
  attr_accessible :hash_key, :name 
  has_many :trails 
  validates :hash_key, :name, :presence => true 
end 

class Trail < ActiveRecord::Base 
  # attr_accessible :title, :body 
  has_and_belongs_to_many :features 
  validates :title, :presence => true    
end

It appears this is somehow being caused by the searchlogic gem. I have this in my Gemfile:

gem 'rd_searchlogic', :require => 'searchlogic', :git => 'git://github.com/railsdog/searchlogic.|~                                                                                                    

When i comment out that line, run bundle install and retry app.get things work fine. So searchlogic is somehow interfering with Trail.all. Why won't Trail.all work with searchlogic installed?

The rd_searchlogic gem was the source of the issue. http://kiranb.scripts.mit.edu/blog/?p=247 talks about the issue. "Any named scope Searchlogic creates is dynamic and created via method_missing. And because Rails 3.1 changed around activerecord so much, Searchlogic calls a missing method on activerecord, which then gets rerouted to searchlogic."

I decided to switch to meta_where, only to learn that it's not supported from Rails 3.1 onwards. I'm running Rails 3.2.8. Squeel is the replacement, and works great on Rails 3.2.8: https://github.com/ernie/squeel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM