草庐IT

ActiveRecord

全部标签

ruby-on-rails - Ruby on Rails 事件记录查询(.each、.collect、.map ...?)

这是我的数据库中一个条目的一个示例:Marketid:1,name:"IndependencePark(IndependentlyRunFarmersMarket...",address:"3945N.SpringfieldAve.,Chicago,IL",zipcode:"60618",created_at:"2013-01-0121:22:24",updated_at:"2013-01-0121:22:24"我只想列出我数据库中所有条目的43个邮政编码。为什么这些查询不起作用?Market.all.each{|m|m.zipcodeMarket.all.zipcodem=Marke

ruby-on-rails - ruby rails : before_save fields to lowercase

我试图在将字段保存到数据库之前将表单中的字段更改为小写。这是我的代码,但数据库的输出仍然是大写的,为什么代码不起作用?classTransaction 最佳答案 downcase返回字符串的副本,不修改字符串本身。使用downcase!代替:defdowncase_fieldsself.name.downcase!end参见documentation了解更多详情。 关于ruby-on-rails-rubyrails:before_savefieldstolowercase,我们在Stac

ruby-on-rails - ruby rails : before_save fields to lowercase

我试图在将字段保存到数据库之前将表单中的字段更改为小写。这是我的代码,但数据库的输出仍然是大写的,为什么代码不起作用?classTransaction 最佳答案 downcase返回字符串的副本,不修改字符串本身。使用downcase!代替:defdowncase_fieldsself.name.downcase!end参见documentation了解更多详情。 关于ruby-on-rails-rubyrails:before_savefieldstolowercase,我们在Stac

ruby-on-rails - rails sqlite 适配器错误

我正在按照railstutorial中的说明进行操作并在尝试使用脚手架命令时卡住了。运行时:railsgeneratescaffoldUsername:stringemail:string我得到错误:C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in`rescueinestablish_connection':Pleaseinstall

ruby-on-rails - rails sqlite 适配器错误

我正在按照railstutorial中的说明进行操作并在尝试使用脚手架命令时卡住了。运行时:railsgeneratescaffoldUsername:stringemail:string我得到错误:C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in`rescueinestablish_connection':Pleaseinstall

database - 在 Rails 中销毁/删除数据库

是否可以从现有应用程序中完全删除数据库和所有迁移记录等,以便我可以从头开始重新设计数据库? 最佳答案 通过发出rake-T你有以下数据库任务:rakedb:create#CreatethedatabasefromDATABASE_URLorconfig/database.ymlforthecurrentRails.env(usedb:create:alltocreatealldbsintheconfig)rakedb:drop#DropsthedatabaseusingDATABASE_URLorthecurrentRails.en

database - 在 Rails 中销毁/删除数据库

是否可以从现有应用程序中完全删除数据库和所有迁移记录等,以便我可以从头开始重新设计数据库? 最佳答案 通过发出rake-T你有以下数据库任务:rakedb:create#CreatethedatabasefromDATABASE_URLorconfig/database.ymlforthecurrentRails.env(usedb:create:alltocreatealldbsintheconfig)rakedb:drop#DropsthedatabaseusingDATABASE_URLorthecurrentRails.en

ruby - 尝试保存大(ish)整数值时出现错误指示数字为 "out of range for ActiveRecord::Type::Integer with limit 4"

我在我的Ruby应用程序中使用SQLite+ActiveRecord,这是我在尝试将大数字写入整数字段时遇到的错误:1428584647765isoutofrangeforActiveRecord::Type::Integerwithlimit4但是根据SQLite文档:Thevalueisasignedinteger,storedin1,2,3,4,6,or8bytesdependingonthemagnitudeofthevalue.8个字节足以存储整数1428584647765,那么为什么ActiveRecord会给我一个错误?为什么它认为这是一个4字节的字段?

ruby - 尝试保存大(ish)整数值时出现错误指示数字为 "out of range for ActiveRecord::Type::Integer with limit 4"

我在我的Ruby应用程序中使用SQLite+ActiveRecord,这是我在尝试将大数字写入整数字段时遇到的错误:1428584647765isoutofrangeforActiveRecord::Type::Integerwithlimit4但是根据SQLite文档:Thevalueisasignedinteger,storedin1,2,3,4,6,or8bytesdependingonthemagnitudeofthevalue.8个字节足以存储整数1428584647765,那么为什么ActiveRecord会给我一个错误?为什么它认为这是一个4字节的字段?

ruby-on-rails - 按指定顺序按 id 查找 ActiveRecord 对象的简洁方法

我想在给定一组id的情况下获得一组ActiveRecord对象。我以为Object.find([5,2,3])将返回一个数组,依次为对象5、对象2、对象3,但我得到的数组顺序为对象2、对象3,然后是对象5。ActiveRecord库findmethodAPI提到您不应该按照提供的顺序期望它(其他文档没有给出此警告)。Findbyarrayofidsinthesameorder?中给出了一个潜在的解决方案,但顺序选项似乎对SQLite无效。我可以自己编写一些ruby​​代码来对对象进行排序(要么比较简单但缩放比例不佳,要么缩放比例更好但更复杂),但是有更好的方法吗?