Friendly_id 不适用于 sql server 和tiny_tds?
我正在使用Friendly_id 4.0.0 Rails 3.0.1和tiny_tds 0.2.3(因为我有一个sql server数据库作为rails应用程序的后端)
在我的模型中,
extend FriendlyId
friendly_id :friendly_name, :use => [:slugged]
当我尝试从rails为类创建新记录时在控制台中,我收到以下错误:
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
这是因为 SQL Server 没有 LENGTH 函数,但有 LEN 函数。
因此,如果我将冲突方法中 slug_generator.rb 中第 48 行的查询更改为:
scope = scope.order("LEN(#{column}) DESC, #{column} DESC")
创建工作。
但是,如果我运行 find_each(&:save) 来为数据库中的现有记录生成段,它仍然不起作用。
是否需要进行一些其他配置更改才能使amily_id与使用tiny_tds的sql server一起工作?
这是堆栈跟踪:
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'
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据此问题,Sql 服务器当前不支持友好 ID。
https://github.com/norman/Friendly_id/issues/214#issuecomment-3528842
Sql server is not currently supported for friendly id as per this issue.
https://github.com/norman/friendly_id/issues/214#issuecomment-3528842
所以看来他们已经做出了改变。因此,您需要在 gemfile 中指定 beta 版本。
就像这样...
然后构建安装,重新启动你的网络服务器,你就可以开始了!
So it appears they've made a change. So you need to specify the beta version in your gemfile.
Like so...
Then build install, restart your web server and you're good to go!