Question: I have a controller function that add columns to table 'custom\_fiels'.
Columns to add comes from table 'custom\_field\_descs'
I want to test that this function adds all expected columns to the table.

My test look like this :

```
describe 'GET /update_db' do
  let(:custom_field) { CustomField.create! valid_attributes }

  before :each do
    FactoryBot.create(:custom_field_desc)
    FactoryBot.create(:custom_field_desc, :type_a)
    FactoryBot.create(:custom_field_desc, :type_b)

    custom_field
  end

  after :each do
    # ...
  end

  it 'updates the database with new columns' do
    get custom_fields_update_db_path

    expect(custom_field.reload).to respond_to(:custom_1)
    expect(custom_field).to respond_to(:custom_2)
    expect(custom_field).to respond_to(:custom_3)
    expect(custom_field).to_not respond_to(:custom_4)
    expect(response).to redirect_to(custom_fields_url)
  end
end

```

The test pass, but I don't know how to remove columns from db after the test.
Datas are not removed after this test (the user, the custom field, etc)
The next time I throw the test, the columns are already there.
and all Tests do not pass because old datas are still in database.

I tried with DatabaseCleaner but it destroy only datas, not created columns.
I tried to add ActiveRecord::Base.connection.remove\_column :custom\_fields, :custom\_1
in after:each but it does not delete columns.

Is it the wright way to test this ?

How can I delete the columns from db for the next test ?

Ruby 2.7
Rails 6
RSpec 4

Answer: 
Javascripts `this` context changes inside event listeners. You can work around this by adding the this context to a seperate variable:

```
class Dialog{
    constructor(id){
        this._dialogBox = $('.dialog#'+this.id);
        this._closeButton = this._dialogBox.find('span.closedialog');

        const self = this;

        this._closeButton.on('click', function(){
            //how to access hide() function from here?
            self.hide()
        });

    }
    hide(){
        this._dialogBack.hide();
        this.dialogBox.hide();
    }
}

```

As you can see I assign the `this` context to the variable `self`. After that you can access the class context with self.
Change the onclick function's listener to arrow function if you are using es6+.. arrow functions don't override `this`

```
class Dialog{
    constructor(id){
        this._dialogBox = $('.dialog#'+this.id);
        this._closeButton = this._dialogBox.find('span.closedialog');

        this._closeButton.on('click', () => {
           this.hide();
        });

    }
    hide(){
        this._dialogBack.hide();
        this.dialogBox.hide();
    }
}````

```