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 7622103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:22:04+00:00 2026-05-31T04:22:04+00:00

I have a Rails app which has 2 databases. Legacy DB with table called

  • 0

I have a Rails app which has 2 databases.

  1. Legacy DB with table called : Businesses
  2. regular development DB that comes with the rails app.

I have data in the Businesses table that I want to put into the development DB. To accomplish this I have taken the following steps:

  1. Set up the app so that I can read from the Businesses DB and see the output in the browser.
    I accomplished this by creating a model Business and a BusinessesController class which reads all the data from the businesses table and stores the entries in an instance variable @businesses

  2. Then I created a model called Listing and a ListingsController. I would like to read all the entries from @businesses in the BusinessesController and store them in @listings in the ListingsController.

Thus essentially all I need to do is take data stored in one instance variable and save it in another instance variable. I’m not sure how to do this in Rails.

So far I have the following classes:

Buisiness

class Business < ActiveRecord::Base  
  establish_connection "Listings_development"
end

class BusinessesController < ApplicationController
  def get_all
    @businesses = Business.all
  end

  def index
    self.get_all
    respond_to do |format|
      format.html #index.html.erb
    end
  end
end

index.html.erb

<h1>Listing businesses</h1>

<table>
  <tr>
    <th>Index</th>
    <th>Name</th>
    <th>Phone Number</th>
    <th>Suite</th>
    <th>Address</th>
    <th>City</th>
    <th>Province</th>
    <th>Postal Code</th>
    <th>Fax</th>
    <th>Latitude</th>
    <th>Longitude</th>
    <th>Website</th>
  </tr>

  <% count = 0 %>
  <% @businesses.each do |business| %>
  <!--<%=business.inspect %> <br> <br>-->
  <%count = count.to_i + 1 %> 
  <tr>
    <td><%= business.bid %></td>
    <td><%= business.company_name %></td>
    <td><%= business.phone_number %></td>
    <td><%= business.suite_number %></td>
    <td><%= business.address %></td>
    <td><%= business.city %></td>
    <td><%= business.province %></td>
    <td><%= business.postal_code %></td>
    <td><%= business.fax_number %></td>
    <td><%= business.latitude %></td>
    <td><%= business.longitude %></td>
    <td><%= business.website %></td>    
  <% end %>
</table>

<br />
<%= link_to 'New Business', new_business_path %>

Listings

class Listing < ActiveRecord::Base
  attr_accessor :name, :telephone

  def initialize(attributes = {})
    @name = attributes[:name]
    @telephone = attributes[:telephone]
    @latitude = attributes[:latitude]
    @longitude = attributes[:longitude]

    puts 'Created a new Listing'
  end
end

class ListingsController < ApplicationController
  def get_all
    @listings = @businesses
    # @listings = businesses_controller.get_all
  end

  def index   
    self.get_all

    respond_to do |format|
      format.html #index.html.erb
    end
  end
end

index.html.erb

<h1>Listings</h1>

<p>This is where all Listings will show up</p>
<%= @listings.inspect %> <br/>
<%= @businesses.inspect %>

When I go to url

http://localhost:3000/businesses

I can see the table of all the entries in my legacy DB – businesses table in the browser

But when I go to url:

http://localhost:3000/listings

I just see the place holder text and for values of @businesses & @listings I see nil.

So clearly the @businesses variable is not accessible within the ListingsController class. I’m wondering how to best pass data between these 2 controllers.

  • 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-31T04:22:05+00:00Added an answer on May 31, 2026 at 4:22 am

    You are doing it wrong! You never should need a controller to talk to another controller in a MVC pattern. Your model should be the only responsible to know how to get the data, and so you would use that model in both controllers. Business logic lives at models.

    You should use Business model inside Listings controller

    @listings = Business.all
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a Rails app running which displays data that is generated by
I have a rails app which has data in MySQL db. I am using
I have rails app which has a list of users. I have different relations
I have a rails app which requires users to verify that they own a
I have a Rails 2.3.11 app which has two key Models: Activity Transaction Live
I have a rails app which currently has 3 models: Post , User and
I have a rails app that has picked up a bit of traction, but
I have rails app which are working perfectly in the local computer. But when
So I have a Rails app (which in this case seems like it would
I have a sequence of migrations in a rails app which includes the following

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.