Rails connection to SQL Server (ZT and supplement)

Source: Internet
Author: User
This is from the jouy International blog, which is listed separately from the book. Try it on your own.
Rails provides good support for My SQL and can be connected without any configuration. However, SQL server connections are cumbersome.
The following describes how to use an ADO driver provided by ruby-DBI for connection.
If one-click installer is installed to install Ruby, all required packages for connecting to SQL Server are installed. However, ADO driver is not installed.
Install it as follows:
1. Find this directory in the ruby Directory: \ Ruby \ Lib \ Ruby \ site_ruby \ 1.8 \ DBD
For example, my ruby is installed in D: \ Ruby, so the Directory D: \ Ruby \ Lib \ Ruby \ site_ruby \ 1.8 \ DBD is used.
Create an ADO folder in this directory
2. Download ruby-DBI
3. Copy the LIB/dbd_ado/ADO. RB file to X:/Ruby/lib/Ruby/site_ruby/1.8/DBD/ADO. RB.
Patch X: \ Ruby \ Lib \ Ruby \ gems \ 1.8 \ gems \ activerecord-1.10.1 \ Lib \ active_record \ connection_adapters \ sqlserver_adapter.rb File
Select column_name as... changed
Select lower (column_name)...
Change Record [col] = row [col] to record [col. downcase] = row [col]
The red section in Agile web development with rails does not exist.
Configure database. yml:
Development:
Adapter: sqlserver
Database: database_name
HOST: SERVER_NAME
Username: user_name
Password: your_pw_here

In this way, you can connect.

You can use the above modifications to connect to and use the SQL Server database, but if you use "rake DB: migrate", you may encounter the following problems:

-- create_table ("xxx_groups", {: force => true})
rake aborted!
DBI: databaseerror: Execute
OLE error code: 80040e14 in Microsoft ole db provider for SQL Server
3rd columns or parameters: the column width cannot be specified for the Data Type Int.
hresult error code: 0x80020009
An error occurred.: Create Table xxx_groups ([ID] int not null identity (1, 1)
Primary Key, [built_in] bit default 0 not null, [parent_id] int (11 ), [private_u
ser_id] int (11), [private_component_id] int (11), [name] varchar (192) default ''
not null, [description] Text default ''not null, [created_on] datetime not null
, [updated_on] datetime not null)

From the SQL statement generated above, we can see the problem. Int also defines the limit. In SQL Server, the length of int is fixed.
Modify the sqlserver_adapter.rb File
Locate column_type_ SQL <"(# {limit})" If limit (rails 1.1.6 is in row 477th)
Change
Column_type_ SQL <"(# {limit})" If Limit & Native [: Name]! = "Int"
Cause: sqlserver_adapter.rb has already been mentioned before.
# SQL Server only supports limits on * char and float types
In this way, rake DB: migrate also works.

BTW,
In fact, rails can communicate with SQL Server not only in windows, but also in Linux and Mac OSX. The related resources are as follows:
Howtoconnecttomicrosoftsqlserver
Howtoconnecttomicrosoftsqlserverfromrailsonlinux
Howtoconnecttomicrosoftsqlserverfromrailsonosx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.