# Active Record and SQL Server

**URL:** <https://discuss.rubyonrails.org/t/active-record-and-sql-server/62073>\
**Category:** rubyonrails-talk\
**Created:** [December 21, 2011, 2:47pm UTC](https://discuss.rubyonrails.org/t/active-record-and-sql-server/62073 "2011-12-21T14:47:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 21, 2011, 2:47pm UTC](https://discuss.rubyonrails.org/t/active-record-and-sql-server/62073/1 "2011-12-21T14:47:11Z")

</div>

I have a Rails application that is set up to use a MySQL database for most of its data but I have a need to pull in data from a SQL Server 2008 instance as well. This holds our data for Microsoft's CRM and GP and therefore cannot be pulled into the MySQL database. So I set up a database configuration in config/database.yml for the connection to this server and the connection seems to work just fine.

With that said, I have a couple of queries that I need to execute on this database which joins together two tables and pulls columns from each table. So I set up a model file for one of the tables like the following...

---

<div class="post-metadata">

**Author:** ![Brynjolfur\_Thorvards](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@Brynjolfur\_Thorvards](https://discuss.rubyonrails.org/u/Brynjolfur_Thorvards)\
**Post date:** [December 21, 2011, 2:53pm UTC](https://discuss.rubyonrails.org/t/active-record-and-sql-server/62073/2 "2011-12-21T14:53:40Z")

</div>

Maybe a simple solution would be to create a view in SQL Server and a model based on that view in Rails?

-----Oprindelig meddelelse-----

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 21, 2011, 3:47pm UTC](https://discuss.rubyonrails.org/t/active-record-and-sql-server/62073/3 "2011-12-21T15:47:01Z")

</div>

Brynjolfur Thorvardsson wrote in post #1037688:

> Maybe a simple solution would be to create a view in SQL Server and a model based on that view in Rails?

That would be a simple solution and one that I have thought about. However, in the future I am considering redeveloping the entire database and using SQL Server as the main platform. I wouldn't want to have to create a bunch of views just to get this to work. I thought the whole point of the Active Record/Model abstraction was so you could code it once and have it work for any database type.

-Michael

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [December 21, 2011, 4:10pm UTC](https://discuss.rubyonrails.org/t/active-record-and-sql-server/62073/4 "2011-12-21T16:10:00Z")

</div>

I guess the problem is that the select statement returns a collection of Sop10200 objects, so there is nowhere to put the data from the other table. Normally in rails one would have relationships between the tables so that the joined data has somewhere to go.

find\_by\_sql might help, have a look at [Railsrocket.com is for sale | HugeDomains](http://www.railsrocket.com/find_by_sql).

Colin
