In one of my application, there is a table, Product with more than 100 columns. I want to find out
- the data type of "product_owner" column
- to iterate over all columns and collect array of boolean / tinyint(1) data type columns.
Here is the code to do that:
Product.columns_hash["product_owner"].sql_type
Above code will give you the data type of the column, in this case its "varchar(255)"
If you know that "product_owner" is the 9th column of the table, then you can also find the data type by command:
Product.content_columns[9].sql_type
To iterate over all columns and collect array of boolean / tinyint(1) data type columns:
Now, when you will print 'b', you will get array of all the fields of data type boolean / tinyint(1)
- the data type of "product_owner" column
- to iterate over all columns and collect array of boolean / tinyint(1) data type columns.
Here is the code to do that:
Product.columns_hash["product_owner"].sql_type
Above code will give you the data type of the column, in this case its "varchar(255)"
If you know that "product_owner" is the 9th column of the table, then you can also find the data type by command:
Product.content_columns[9].sql_type
To iterate over all columns and collect array of boolean / tinyint(1) data type columns:
b= []
Product.content_columns.each do |p|
if p.sql_type == "tinyint(1)"
b << p.human_name
end
end
Now, when you will print 'b', you will get array of all the fields of data type boolean / tinyint(1)
No comments:
Post a Comment