I’ve been working on a project with a has_many :through relationship between a Story and a Stack model, joined by Anthologies. I recently tweaked something that has broken the create method for new stories, but I’m not sure how to troubleshoot it. When I go to add a story, I get the following error:
**NoMethodError in StoriesController#create**
undefined method `stack_id' for #<Story:0x1035968a8>
What’s weird is that the create method makes no reference to ‘stack_id’. In fact, it’s pretty out of the box:
def create
@story = Story.new(params[:story])
respond_to do |format|
if @story.save
flash[:notice] = 'Story was successfully created.'
format.html { redirect_to(@story) }
format.xml { render :xml => @story, :status => :created, :location => @story }
else
format.html { render :action => "new" }
format.xml { render :xml => @story.errors, :status => :unprocessable_entity }
end
end
end
I thought maybe it had something to do with the routes, as they support accessing stories both directly and nested in stacks. But I’ve tried a number of variations and nothing seems to make a difference.
map.resources :stories, :member => {:rate => :post}
map.resources :anthologies
map.resources :stacks, :has_many => :stories do |stack|
stack.resources :stories
end
I’m clearly not understanding something about how objects are getting saved here. Any ideas on how I can solve this problem? I’m completely lost on what to do next. I’ve added the console output below in case it’s helpful.
Thanks.
Processing StoriesController#create (for 127.0.0.1 at 2010-11-17 21:45:29) [POST]
Parameters: {"commit"=>"save", "story"=>{"body"=>"sgfhsfgh", "title"=>"asdfa", "user_id"=>"1", "summary"=>"adfgdfhg"}, "action"=>"create", "authenticity_token"=>"1yws3Fs108kPSUS2W5SM1GFaiRqfiWhDbzDWstfoDCA=", "controller"=>"stories"}
User Load (0.3ms) SELECT "users".id FROM "users" WHERE ("users"."id" = 1) LIMIT 1
NoMethodError (undefined method `stack_id' for #<Story:0x1035968a8>):
(eval):13:in `send'
(eval):13:in `scope_condition'
app/controllers/stories_controller.rb:58:in `create'
app/controllers/stories_controller.rb:57:in `create'
Rendered rescues/_trace (90.0ms)
Rendered rescues/_request_and_response (0.3ms)
Rendering rescues/layout (internal_server_error)
Adding Model code by request, but it’s pretty generic has_many :through…
has_many :anthologies
has_many :stories, :through => :anthologies
and
has_many :anthologies
has_many :stacks, :through => :anthologies
Based on your routes I’d expect your stories table has a stack_id and the model has something like:
Do you have a stack_id field in the stories table?