开发者

Stack Level Too Deep - Re: has_many to belongs_to Association

开发者 https://www.devze.com 2023-04-01 08:17 出处:网络
Here\'s my code: #class CategoriesController < ApplicationController def show @category = Category.find(params[:id])

Here's my code:

#class CategoriesController < ApplicationController
def show
  @category = Category.find(params[:id])
end

#class Category < ActiveRecord::Base
has_one :task_list

#TaskList < ActiveRecord::Base
belongs_to :category

With a category_id in the TaskList model.

I'm putting this in my view:

<%= @category.task_list.id %>

And I get a stack level too deep error.

I'm lost and I swear I've done this same setup 100 times over.

Any help would be greatly appreciated. Thanks.

Update:

Here's the full trace (minus the top i trimmed cause it was repeating over and over again:

See gist link below

Update 2:

Ok I've started from scratch and refined somethings I didn't need and am doing this exactly as it shows: http://asciicasts.com/episodes/196-nested-model-form-part-1

class Category < ActiveRecord::Base
  has_many :tasks, :dependent => :destroy
end

class Task < ActiveRecord::Base
  belongs_to :category
end

#_form.html.erb
<%= form_for @category do |f| %>
    <%= f.error_messages %>
    <p>
        <%= f.label :name %><br />
        <%= f.text_field :name %>
    </p>

    <p>
        <% f.fields_for :tasks do |builder| %>  
        <p>  
            <%= builder.text_field :name %>  
        </p>  
        <% end %>  

    </p>

    <p><%= f.submit %></p>
<% end %>

#CategoryController 
def new
  @category = Category.new
  3.times { @category.tasks.build }  
end

And I'm still getting the same th开发者_Go百科ing... Here's the full https://gist.github.com/1185772

Update 3:

Here's the full version of the Task & Category models: https://gist.github.com/1185839


Have you checked your models?

If both are set to :destroy, then you'll end up with an infinite loop and the stack level too deep error. They should be as follows:

has_one :task_list, :dependent => :destroy

belongs_to :category, :dependent => :delete

There's more info. about the error here.


See the comment that diedthreetimes left on the question. Acts_as_tree was having an issue with the scopes that I had made. And I also ended up starting from scratch and it's working fine now.


The problem is that task is a reserved word in Rails. Here is what task output when executing in a clean app console:

irb(main):001:0> task
=> <Rake::Task  => []>

Rename your model and it will work.


Your gist shows Tasks < ActiveRecord when it should be Task < ActiveRecord

Did you generate your model properly like so:

rails g model Task category_id:integer title:string
0

精彩评论

暂无评论...
验证码 换一张
取 消