Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:07:30+00:00 2026-05-27T02:07:30+00:00

I want to iterate over all the columns in a table for a Model

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:07:31+00:00Added an answer on May 27, 2026 at 2:07 am

    The most obvious way is to use the columns method, for example:

    > User.columns.each { |c| puts "#{c.name} - #{c.sql_type} - #{c.type}" }
    id - INTEGER - integer
    email - varchar(255) - string
    crypted_password - varchar(255) - string
    password_salt - varchar(255) - string
    persistence_token - varchar(255) - string
    login_count - integer - integer
    failed_login_count - integer - integer
    last_request_at - datetime - datetime
    current_login_at - datetime - datetime
    last_login_at - datetime - datetime
    current_login_ip - varchar(255) - string
    last_login_ip - varchar(255) - string
    created_at - datetime - datetime
    updated_at - datetime - datetime
    nickname - varchar(255) - string
    first_name - varchar(255) - string
    last_name - varchar(255) - string
    

    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 reflections method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a String with some value. I want to iterate over all classes
I have a boost::bimap and I want to iterate over all positions to add
I have a Hashtable in Java and want to iterate over all the values
I want to iterate through a table/view and then kick off some process (e.g.
Specifically, I want to iterate over every element on the page, each time the
I'm converting diverse JSON objects to structured objects and want to iterate over all
I have a filehandle FILE in Perl, and I want to iterate over all
In a bash script, I want to iterate over all the directories in the
I want to iterate over all childs of a jQuery's .children() return value, like
I want to iterate over a sequence in xquery and grab 2 elements at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.