gpt4 book ai didi

ruby-on-rails - Ruby on Rails 向 "find"函数添加双引号

转载 作者:行者123 更新时间:2023-12-04 05:49:41 27 4
gpt4 key购买 nike

我是 ruby​​ on rails 的新手,我在使用 find 函数时遇到了问题。

我创建了一个新的 Web 应用程序,它使用 oracle 增强型适配器 gem 连接到旧的 Oracle 数据库。

当我在 rails 控制台中运行以下命令时,我从数据库中取回数据

License.where(license_no: 'L1234') 

或者
License.find_by_license_no('L1234') 

但是,当我尝试使用 find() 函数时,出现错误。 IE。
License.find(:all)

或者
License.all
rails控制台中的错误输出如下:
irb(main):003:0> License.find(:all)
?[1m?[35mLicense Load (1062.5ms)?[0m SELECT "LICENSE".* FROM "LICENSE"
ActiveRecord::StatementInvalid: OCIError: ORA-01878: specified field not found i
n datetime or interval: SELECT "LICENSE".* FROM "LICENSE"
from stmt.c:289:in oci8lib_191.so
from c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activerecord-o
racle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanc
ed_oci_connection.rb:155:in `fetch'
from c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activerecord-o
racle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanc
ed_adapter.rb:637:in `block in exec_query'
from c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activerecord-3
.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in
log'

查看错误输出中的 SQL 命令,我注意到双引号被添加到 select 语句中。当我尝试在 SQL Developer 中运行 SQL 命令时,出现错误。添加双引号有什么原因吗?有没有办法阻止 ruby​​ on rails 在表名中添加双引号?

更新 oracle 数据库是 10g,我使用 rails 3.2 和 ruby​​ 1.9.2p290。我也尝试了以下并得到了同样的错误。 License.all

最佳答案

在 Rails 3 中使用 find 函数(取决于您使用的 Rails 版本)的正确格式是:

License.where('license_no = ?', 'L1234')


License.all

找到所有这些。

如果您在 Rails 2 中,格式将是:
License.find(:all, :conditions =>  ["license_no = ?", "L1234"])

基于动态属性的查找器 部分在这里:

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

关于ruby-on-rails - Ruby on Rails 向 "find"函数添加双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10229093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com