friendly_id does not work with sql server and tiny_tds?

570 views Asked by At

I am using friendly_id 4.0.0 rails 3.0.1 and tiny_tds 0.2.3 (because I have a sql server database as a backend for the rails application)

In my model I have

extend FriendlyId

friendly_id :friendly_name, :use => [:slugged]

When I try to create a new record for the class from rails console, I get the following error:

ActiveRecord::StatementInvalid: TinyTds::Error: 'LENGTH' is not a recognized built-in function name.: SELECT TOP (1) [j_service_provider].* FROM [j_service_provider] WHERE ([slug] = N'' OR [slug] LIKE N'--%') ORDER BY LENGTH([slug]) DESC, [slug] DESC

This is because SQL Server does not have the LENGTH function, but is has a LEN function.

So if I change the query on line 48 in slug_generator.rb in conflicts method to:

scope = scope.order("LEN(#{column}) DESC, #{column} DESC")

the create works.

However to generate slugs for existing records in database if I run the find_each(&:save) it still does not work.

Is there some other configuration change needed to make friendly_id work with sql server using tiny_tds?

This is the stack trace:

ActiveRecord::StatementInvalid: TinyTds::Error: 'LENGTH' is not a recognized built-in function name.: SELECT TOP (1) [company].* FROM [company] WHERE ([slug] = N'' OR [slug] LIKE N'--%') ORDER BY LENGTH([slug]) DESC, [slug] DESC
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `log'
from /usr/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-3.0.7/lib/active_record/connection_adapters/sqlserver/database_statements.rb:249:in `raw_select'
from /usr/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-3.0.7/lib/active_record/connection_adapters/sqlserver/database_statements.rb:193:in `select'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:56:in `select_all'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/base.rb:467:in `find_by_sql'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation.rb:64:in `to_a'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation/finder_methods.rb:333:in `find_first'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation/finder_methods.rb:122:in `first'
from /usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.0/lib/friendly_id/slug_generator.rb:38:in `conflict'
from /usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.0/lib/friendly_id/slug_generator.rb:33:in `conflict?'
from /usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.0/lib/friendly_id/slug_generator.rb:23:in `generate'
from /usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.0/lib/friendly_id/slugged.rb:257:in `set_slug'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:424:in `_run_validation_callbacks'
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.1/lib/active_model/validations/callbacks.rb:67:in `run_validations!'
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.1/lib/active_model/validations.rb:179:in `valid?'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/validations.rb:55:in `valid?'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/validations.rb:75:in `perform_validations'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/validations.rb:43:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/attribute_methods/dirty.rb:21:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:237:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:289:in `with_transaction_returning_status'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:204:in `transaction'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:237:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:248:in `rollback_active_record_state!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:236:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/base.rb:498:in `create'
2

There are 2 answers

0
ami On

Sql server is not currently supported for friendly id as per this issue.

https://github.com/norman/friendly_id/issues/214#issuecomment-3528842

0
daveomcd On

So it appears they've made a change. So you need to specify the beta version in your gemfile. Like so...

gem 'friendly_id', '4.1.0.beta.1'

Then build install, restart your web server and you're good to go!