Reputation: 1370
So the full error is:
Failure/Error: @region = Region.find_by!(url_name: params[:id]) ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column regions.url_name does not exist Line 1: SELECT "regions".* FROM "regions" WHERE "regions"."url_name...
Then as a failed example I get:
rspec ./spec/controllers/regions_controller_spec.rb:15 # RegionsController show should render the show page
In my regions controller I have for show:
def show
@region = Region.find_by!(url_name: params[:id])
@careers = @region.careers
end
For my regions spec controller I have:
describe 'show' do
it 'should render the show page' do
region = Region::Create.run(FactoryBot.attributes_for(:region)).result
get :show, params: { id: region.url_name }
expect(response).to have_http_status :success
end
it 'should return a 404 error' do
get :show, params: { id: 1 }
expect(response).to have_http_status :not_found
end
end
In the model for regions I have:
class Region < ApplicationRecord
scope :ordered, -> { order(name: :desc) }
scope :search, ->(term) { where('name LIKE ?', "%#{term}%") }
has_many :locations, dependent: :destroy
has_many :careers, through: :locations
validates :name, presence: true
end
And in my schema I have:
create_table "regions", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "url_name"
end
So the column exists. The page loads using the url_name. It's however erroring and telling me that the column doesn't exist...?
Upvotes: 1
Views: 1211
Reputation: 1370
So it looks as though url_name was present in the schema but another person removed the migration file. To resolve I ran a remove column migration then did an add column migration and it resolved the problem.
Upvotes: 1