Getting undefined method for ActiveRecord::Relation
Looks like @chapter is not a single Chapter object. If @chapter is initialized something like this:
@chapter = Chapter.where(:id => params[:id])
then you get a Relation object (that can be treated as a collection, but not a single object). So to fix this you need to retrieve a record using find_by_id
, or take a first one from the collection
@chapter = Chapter.where(:id => params[:id]).first
or
@chapter = Chapter.find_by_id(params[:id])
As the others have said - adding the .first
method will resolve this. I have experienced this issue when calling a @chapter by it's unique ID. Adding .first
(or .take
in Rails 4) will ensure only one object is returned.
Try: Chapter.find(params[:id]).first