I want to iterate over all the columns in a table for a Model and perform some logic on it.
def fetch_all_fields
@profile_page ||= profile_page
SOMETHING.each do |field_name|
method_name = "remote_#{field_name}"
if self.respond_to?(method_name, true)
field_values[field_name] = send(method_name)
end
end
field_values
end
Basically, a simple loop in my model that allows me to define methods like remote_date_of_birth. This method then contains the correct logic and information to parse the date_of_birth from some remote dataset (it actually scrapes HTML).
The part that I cannot find, is how to get the “columns” for the table for this Model. Say a table profiles, that, in abovementioned example, has a column date_of_birth.
The most obvious way is to use the
columnsmethod, for example:If you need them in an arbitrary, non-sorted order, the hash is more appropriate.
This does not include the associations, which can be found via the
reflectionsmethod.