in Education by
I have a company table and I want to check the existence of multiple column to remove them. class RemoveSettingsReportColumnsFromCompany < ActiveRecord::Migration[5.1] def change if column_exists?( :companies, :automatically_send_report ) && column_exists?( :companies, :time_limit_for_sending_report ) && column_exists?( :companies, :report_fragment_color ) && column_exists?( :companies, :display_default_avatar ) remove_column :companies, :automatically_send_report, :boolean remove_column :companies, :time_limit_for_sending_report, :integer remove_column :companies, :report_fragment_color, :string remove_column :companies, :display_default_avatar, :boolean end end end As you see there is a redundancy of use of the function column_exists, and I want to better organize and reduce my code, can some one tell if there is a way to use column_exists in other way, and give it as parameters all columns I want to remove ? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It is possible to simplify it a little bit. First of all - you can list all attributes using Company.attribute_names. It will return an array. So you can do something like: if (%w[automatically_send_report time_limit_for_sending_report report_fragment_color display_default_avatar] - Company.attribute_names).empty? # migration code end Another solution, a little bit prettier (IMO) is: if Company.attribute_names.to_set.superset?(Set['automatically_send_report', 'time_limit_for_sending_report', 'report_fragment_color', 'display_default_avatar']) # migration code end

Related questions

0 votes
    To configure the admin settings of my app I made a Admin controller and a AdminConfig model. The Admin ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    To configure the admin settings of my app I made a Admin controller and a AdminConfig model. The Admin ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I added a table that I thought I was going to need, but now no longer plan on using it. How ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    I have a Rails app that mounts 3 engines. Each engines has their own db migrations with timestamp in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have a user model that has many fields (let's say 30 for this example). When the user signs ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I've come to this code, but from this i have to manually insert all columns and check it by each ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have data regarding gender of people under 8 columns: mem1;mem2;mem3;mem4;mem5;mem6;mem7;mem8 MALE; ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I have a dataset, say: DateJoined Name Number DateLeft 11/03/2015 Tom 001199 11/03/2019 11/03/ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have the following df | 1 | 2 | 3 | ------------------------- 0.11 ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Which of the following function takes multiple columns? (a) spread() (b) gather() (c) separate() (d) ... Analysis of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Which of the following d takes two columns and spreads them into multiple columns? (a) ggmissplot (b) printplot ... of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    Working with census data, I want to replace NaNs in two columns ("workclass" and "native-country") with the ... way to do this? Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
0 votes
    How to sort a data.table using vector of multiple columns in R? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    Did existence of lyapunov function is necessary for stability? (a) yes (b) no This question was posed to ... Synaptic Dynamics of Neural Networks Please answer the above question....
asked Sep 5, 2022 in Education by JackTerrance
0 votes
    State wheather the following statements are true or false. Give reasons for your answer. Consumer movement came into existence. Please answer the above question....
asked Aug 14, 2022 in Education by JackTerrance
...