bravely
bravely

Reputation: 185

RSpec changing model attribute name

Not the value, the name of the attribute. Yes, for real. I don't know what the hell is going on.

The migration:

class CreateFolders < ActiveRecord::Migration
  def change
    create_table :folders do |t|
      t.string :name, null: false

      t.timestamps
    end

    change_table :bookmarks do |t|
      t.belongs_to :folder
    end
  end
end

The Schema:

ActiveRecord::Schema.define(version: 20140424065045) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "bookmarks", force: true do |t|
    t.string   "name",       null: false
    t.string   "url",        null: false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "folder_id"
  end

  create_table "folders", force: true do |t|
    t.string   "name",       null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

What it shows inside of rails c:

[3] pry(main)> Bookmark
=> Bookmark(id: integer, name: string, url: string, created_at: datetime, updated_at: datetime, folder_id: integer)

And now, our huge glaring problem:

[3] pry(#<RSpec::Core::ExampleGroup::Nested_2::Nested_1>)> Bookmark
=> Bookmark(id: integer, name: string, url: string, created_at: datetime, updated_at: datetime, folders_id: integer)

Notice the name of the last attribute there: folders_id

Does anyone know what in the hell could ever cause this?

Upvotes: 0

Views: 144

Answers (1)

bravely
bravely

Reputation: 185

Finally found what the issue was, and damn is it bizarre.

So brand new in Rails 4, is ActiveRecord::Migration.maintain_test_schema!. This convenient little tool is pretty nice, however it only updates the test schema on creation of a new migration. In the process, if you get a migration wrong the first time, and update it later, you'll find inconsistencies like this.

To fix the problem, run rake db:test:prepare. You'll get a deprecation warning, but ignore it. When you check inside of rspec again it should work fine.

Upvotes: 2

Related Questions