# Model validation with SQL Server

**URL:** <https://www.ruby-forum.com/t/model-validation-with-sql-server/83665>\
**Category:** Rails\
**Created:** [January 28, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/model-validation-with-sql-server/83665 "2007-01-28T16:57:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [January 28, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/model-validation-with-sql-server/83665/1 "2007-01-28T16:57:41Z")

</div>

Hi,

I have a model that looks like this:

class Assessment \< ActiveRecord::Base  
…  
set\_table\_name “SCG\_Assessment”  
set\_primary\_key “ID\_”  
validates\_presence\_of :Name, :Description, :Version  
validates\_length\_of :Name, :maximum =\> 50  
…  
end

My setup is windows server 2003 R2, RoR 1.1.6, and SQL Server 2005.  
When I intentionally enter a string that is greater than 50 characters,  
the write to the database is still attempted, and the user sees an ugly  
error like this:

ActiveRecord::StatementInvalid in AssessmentsController#create

DBI::DatabaseError: Execute  
OLE error code:80040E57 in Microsoft OLE DB Provider for SQL Server  
String or binary data would be truncated.  
HRESULT error code:0x80020009  
Exception occurred.: INSERT INTO SHAKERCG\_Assessment ([Name],  
[Version], [State], [Customer\_ID\_FK], [Description]) VALUES(‘this is my  
new assessment with a really long name.’, ‘1’, NULL, 1, ‘hello’)

RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/abstract\_adapter.rb:120:in  
`log' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/sqlserver_adapter.rb:279:in`insert’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:1739:in  
`create_without_callbacks' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:265:in`create\_without\_timestamps’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/timestamp.rb:30:in  
`create' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1718:in`create\_or\_update\_without\_callbacks’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:253:in  
`create_or_update' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1392:in`save\_without\_validation’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/validations.rb:736:in  
`save_without_transactions' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in`save’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:51:in  
`transaction' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:91:in`transaction’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/transactions.rb:118:in  
`transaction' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in`save’  
#{RAILS\_ROOT}/app/controllers/assessments\_controller.rb:35:in `create’

I want the user to see the flash[:notice] message in a

 element  
that I have created in the page template. Instead, they are directed to  
this type of page. Because this is a legacy schema, the primary key and  
table names must all be overriden. I don’t know this is happening  
because of SQL Server, or because of not being able to use the RoR  
naming conventions.

I also tried wrapping the save call around a begin/rescue block, hoping  
to catch the exception (changing to a .save! vs. .save), but that didn’t  
seem to help either.

Any suggestions?  
-Kevin

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [January 28, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/model-validation-with-sql-server/83665/2 "2007-01-28T17:32:03Z")

</div>

On Sun, 2007-01-28 at 16:57 +0100, Kevin T. wrote:

> …  
> end

Are you sure you don’t want :maximum =\> 49? Your test string was  
exactly 50 characters long, so according to :maximum =\> 50 it’s a valid  
length.

> My setup is windows server 2003 R2, RoR 1.1.6, and SQL Server 2005.

God save you 🙂

–  
Matthew B. :: 607 227 0871  
Resume & Portfolio @ [http://madhatted.com](http://madhatted.com)

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [January 28, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/model-validation-with-sql-server/83665/3 "2007-01-28T18:42:18Z")

</div>

Matthew B. wrote:

> Are you sure you don’t want :maximum =\> 49? Your test string was  
> exactly 50 characters long, so according to :maximum =\> 50 it’s a valid  
> length.

I didn’t realize my input string was 50 characters exactly. I’m  
guessing that the varchar(50) in MSSQL also counts a NULL character,  
which is why the insert fails with exactly 50 characters.

When I enter a string that is longer than 50, I get an exception as I  
would expect. I’m also adding in the error\_messages\_for construct to my  
views, so that errors are displayed more completely. I’ll have to go  
around and subtract one from all my lengths, but other than that things  
are working as expected.

> > My setup is windows server 2003 R2, RoR 1.1.6, and SQL Server 2005.
> 
> God save you 🙂

Yes, I would much rather have a Linux/MySQL setup, but this is what the  
customer wants. oh well.

Thanks again for your help.

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [January 28, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/model-validation-with-sql-server/83665/4 "2007-01-28T17:57:46Z")

</div>

Kevin T. wrote:

> class Assessment \< ActiveRecord::Base  
> …  
> set\_table\_name “SCG\_Assessment”  
> set\_primary\_key “ID\_”  
> validates\_presence\_of :Name, :Description, :Version  
> validates\_length\_of :Name, :maximum =\> 50  
> …  
> end
> 
> I want the user to see the flash[:notice] message in a
> 
> element  
> that I have created in the page template. Instead, they are directed to  
> this type of page. Because this is a legacy schema, the primary key and  
> table names must all be overriden. I don’t know this is happening  
> because of SQL Server, or because of not being able to use the RoR  
> naming conventions.
> 
> Kevin,
> 
> Hmmm. Can you post your rescue block? I can’t think of why something  
> along the lines of
> 
> begin  
> obj.save!  
> rescue  
> return render(:orig.page)  
> end
> 
> wouldn’t work for you?
> 
> Wes
