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

  • Home
  • SEARCH
  • 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 7441453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:00:55+00:00 2026-05-29T11:00:55+00:00

Running Rails 3.1.3… I’ll use a simple example of nested associations (not sure if

  • 0

Running Rails 3.1.3…

I’ll use a simple example of nested associations (not sure if this is correct term). Basically I’m modelling databases – each database has its own tables and each table has its own columns:

class Database < ActiveRecord::Base
  has_many :tables
end

class Table < ActiveRecord::Base
  belongs_to :database
  has_many :columns
end

class Column < ActiveRecord::Base
  belongs_to :table
end

My question is, say I want to display a database’s tables and columns in a view, what would be a good way of bundling this data before passing it to the view. Basically, what would my controller and view look like?

I’ve come up with the following, but I would be surprised if there is not a much better way of doing this:

My controller:

class DatabasesController < ApplicationController
  def show
    @database = Database.find_by_id(params[:id])
    @tables = @database.tables
    @columns = @database.tables.columns
  end
end

My view:

Database: <%= @database.database_name %><br />
<% @tables.each do |table| %>
  Table: <%= table.table_name %><br />
  <% table.columns.each do |column| %>
    Column: <%= column.column_name %><br />
  <% end %>
<% end %>

I’ve also played around with using this in the controller:

@database = Database.where(:id => params[:id]).includes(:tables => [:columns])

However trying to access the table and column names from @database has driven me mad.


UPDATE:

Typically, I have spent many hours trying to figure this out and shortly after I post here, I think I get it. Thanks for the suggestion miked – this worked for me. Also, if I amend my own method by using the first! method it works as follows:

Controller:

def show
  @database = Database.where(:id => params[:id]).includes(:tables => [:columns]).first!
end

View:

Database: <%= @database.database_name %><br />
<% @database.tables.each do |table| %>
  Table: <%= table.table_name %><br />
  <% table.columns.each do |column| %>
    Column: <%= column.column_name %><br />
  <% end %>
<% end %>
  • 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-29T11:00:56+00:00Added an answer on May 29, 2026 at 11:00 am

    Unless I’m missing something, this actually looks okay to me, though you don’t need the @tables and @columns in the controller unless you are really using them for something in the view.

    controller:

    class DatabasesController < ApplicationController
      def show
        @database = Database.find(params[:id], :include=>{:tables => [:columns]}) #eager load
        #or: @database = Database.where(:id => params[:id]).includes(:tables => [:columns]).first #eager load
        #or: @database = Database.find(params[:id]) #queries will be executed in the view
      end
    end
    

    view:

    Database: <%= @database.database_name %><br />
    <% @database.tables.each do |table| %>
      Table: <%= table.table_name %><br />
      <% table.columns.each do |column| %>
        Column: <%= column.column_name %><br />
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error when running rails with mysql db. Basically it is not
So I am getting this error for one of my routes. I'am running Rails
I am running Rails 3.1.1 and getting this error when running this command. Obviously
I have a RoR app running rails 2.3.8 which makes use of the Rails
I am running Rails 3 configured to use memcached for session store. I have
I'm running rails 3 and have installed the acts_as_rateable gem by adding this gem
I am running Rails 2.0.2 and am unable to use :dependent => :delete in
I am running rails 3.1.3 with postgresql 9.1 and testing with Rspec. This is
I'm running Rails 3.0.9 I have this: <%= t time_ago_in_words(i.created_at) %> And it prints:
I am running rails 3.0.0, rspec-rails 2.0.0.beta.20, webrat 0.7.2.beta.1, cucumber-rails 0.3.2 I have this

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.