2012-11-04 74 views
5

我的Rails应用程序有3个模型。路径,地区和特征。我能够在我的lib/tasks目录中很好地与这些模型进行交互。我用anemone爬行并填充数据库。我在模型上拨打的电话示例如下:Rails:堆栈级别太深错误

Trail.find_or_initialize_by_title(detail_title) 

我正在尝试编写使用该模型的控制器。

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

现在,如果我打开轨道控制台,并尝试app.get('trail/index')我得到了500返回码,我看到在我development.log

SystemStackError(堆栈级别太深)以下:
应用程序/控制器/ trail_controller.rb:23:在'index'中

所以我显然会导致一些无限递归。第23行对应于索引方法的主体。我试过我的应用中的其他模型:特征和区域,结果是一样的。有人能告诉我我在这里做错了什么,或者我可以如何得到更多的追踪来确定无限递归是什么?

我的模型很简单:

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 

看来这是某种由searchlogic宝石造成的。我有这个在我的Gemfile:

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

当我注释掉该行,运行捆绑安装,然后重试app.get事情做工精细。所以searchlogic会以某种方式干扰Trail.all。为什么Trail.all不能使用searchlogic安装?

+0

如果你这样做会发生什么:'trails = Trail.all;渲染JSON:Trails'? –

+0

我想你在这些模型之一覆盖了as_json或to_json。 – apneadiving

+0

我试过'trails = Trail.all; render:json => trails'并得到相同的错误。 – theraju

回答

1

rd_searchlogic gem是问题的根源。 http://kiranb.scripts.mit.edu/blog/?p=247谈论这个问题。 “任何命名范围Searchlogic创建的都是动态的,并且通过method_missing创建,并且因为Rails 3.1在activerecord周围发生了很大变化,所以Searchlogic在activerecord上调用缺少的方法,然后将其重新路由到searchlogic。”

我决定切换到meta_where,只是为了了解它不支持从Rails 3.1开始。我正在运行Rails 3.2.8。 Squeel是替代品,并且在Rails 3.2.8上效果很好:https://github.com/ernie/squeel