سؤال

I'm new to Postgres and I'm getting this error when testing the destroy action for a blog-type application. It seems like the problem is that works, rather than work, is being called for a given Author when it's destroyed - as in it's being pluralized, but I'm not sure why that is.

Author.rb  
has_many :works, :dependent => :destroy

Error:

AuthorsController::destroy#test_0001_deletes the author:
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column works.author_id does not exist
LINE 1: SELECT "works".* FROM "works"  WHERE "works"."author_id" = $...
                                             ^
: SELECT "works".* FROM "works"  WHERE "works"."author_id" = $1  ORDER BY works.id ASC

Test:

 it "deletes the author" do
   assert_difference 'Author.count', -1 do
     delete :destroy, :id => author.id
   end
 end

Controller:

def destroy
  @author.destroy
  redirect_to authors_path
end
هل كانت مفيدة؟

المحلول

The issue that Postgres is complaining about is that it can't find a column called author_id on your works table, and as a result, it won't be able to find the corresponding works for a given Author.

If you haven't already, you'll need to add a migration to add an author_id column to Work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top