postgresql - PG::UndefinedColumn after Rails rename column -
pg::undefinedcolumn @ /books/17 error: column "books_count" not exist @ character 45 statement: update "users" set "books_count" = coalesce("books_count", 0) - 1 "users"."id" = $1
i above error after changing running name change in rails.
def change rename_column :users, :books_count, :books_shared end
i first noticed problem when trying delete book. otherwise, works. there column in postgresql have change well? or should revert name change?
i restarted server.
edit: current schema
create_table "users", force: :cascade |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.inet "current_sign_in_ip" t.inet "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" t.string "name" t.integer "books_shared", default: 0 t.integer "books_read", default: 0 end
books_shared books_count. no other instances of books_count
associations
class book < activerecord::base belongs_to :user, counter_cache: true validates :title, presence: true, length: {minimum: 3} validates :author, presence: true has_many :book_checkouts end
by default rails looks pluralized associated model followed _count
, in case books_count
.
looks @ docs belongs_to
here , counter_cache
:
http://api.rubyonrails.org/classes/activerecord/associations/classmethods.html#method-i-belongs_to
you can specify custom column name counter_cache on book class so:
belongs_to :user, counter_cache: :books_shared
Comments
Post a Comment