This might be a little odd, but how can I get the column name from object in my model?
For example, I have a table which happens to have all 50 states in it as separate columns (I know, I know that could be way more efficient), so they look like:
al_allowed, ak_allowed, etc...
These columns are booleans, but I wanted to know in my model how to get the column names?, for example if for a particular record (in this case Campaigns), I want to see which states are checked off, and if they are True, I want to push the states abbreviation(the column name) into an array.
You can get an array of column names from an ActiveRecord model by calling the
column_namesmethod. It’s a class method, not an instance method, so you would useCampaign.column_names.