I have a Rails app which has 2 databases.
- Legacy DB with table called : Businesses
- 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:
-
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 modelBusinessand aBusinessesControllerclass which reads all the data from thebusinessestable and stores the entries in an instance variable@businesses -
Then I created a model called
Listingand aListingsController. I would like to read all the entries from@businessesin theBusinessesControllerand store them in@listingsin theListingsController.
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.
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