草庐IT

existent

全部标签

ruby-on-rails - Ruby on Rails 中 `any?` 和 `exists?` 之间的区别?

在RubyonRails中,似乎有两种方法可以检查集合中是否包含任何元素。即,它们是ActiveRecord::FinderMethods’exists?和ActiveRecord::Relation’sany?.在通用查询(Foo.first.bars.exists?和Foo.first.bars.any?)中运行它们会生成等效的SQL。有什么理由要用一个而不是另一个吗? 最佳答案 #any和#exists?是非常不同的野兽,但查询相似。主要是,#any?接受一个block——通过这个block,它检索关系中的记录,调用#to_a

ruby-on-rails - rails : How do I solve the OCIError: ORA-02289: sequence does not exist error?

我有一个名为“Carrier”的模型。在我的Controller中,当我执行Carrier.create(data)时,出现此错误:OCIError:ORA-02289:sequencedoesnotexist:selectcarriers_seq.nextvalidfromdual这是我Controller中的代码:classCarriersController"index")endend我已经做了一些谷歌搜索,我发现了这两个关于解决方案的网页:http://niranjansarade.blogspot.com/2011/03/avoid-oracle-sequence-durin

ruby-on-rails - ActiveRecord : If exists then delete else create 的最佳方式

我有这条路线可以为某个提示创建收藏夹:deffavorite@tip=Tip.find(params[:id])Favorite.create(user:current_user,tip:@tip)redirect_to:action=>"show",:id=>@tip.idend我希望它更像一个开关。所以:如果用户对某个提示的收藏已经存在,那么它应该删除这个收藏。如果它是用户和提示的新组合,它应该使用这些值创建一个新的收藏夹。最好和最漂亮的方法是什么? 最佳答案 试试这个,我认为这是最简洁的解决方案:deffavorite#Sin

ruby-on-rails - PGError : ERROR: relation "delayed_jobs" does not exist (Postgresql, rails 3.04,delayed_job 错误)

我做了rakedb:create然后rakedb:migrate遇到了这个错误。rakeaborted!PGError:ERROR:relation"delayed_jobs"doesnotexistLINE4:WHEREa.attrelid='"delayed_jobs"'::regclass^:SELECTa.attname,format_type(a.atttypid,a.atttypmod),d.adsrc,a.attnotnullFROMpg_attributeaLEFTJOINpg_attrdefdONa.attrelid=d.adrelidANDa.attnum=d.ad

ruby-on-rails - 使用 Active Record exists 有什么区别?与 find_by 相比?

在我的模型中,我首先检查数据库中是否存在Sudoku记录,然后再获取它的solution或创建它。ifSudoku.exists?(puzzle:puzzle)returnSudoku.find_by(puzzle:puzzle).solutionelse**solveitandsaveitintothedb**end这样做有什么区别:ifSudoku.find_by(puzzle:puzzle)returnSudoku.find_by(puzzle:puzzle).solutionelse**solveitandsaveitintothedb**end的日志是否存在?SudokuEx

ruby - 尝试安装 pg gem : Brew bad interpreter error and not existing ruby version 时

我在重新安装ruby​​、rails、xcode等时确实犯了一些错误(甚至可能我已经删除了我的系统Ruby安装:/)这是我现在拥有的:系统:OSX10.8.2(64位)whichruby​​给我/usr/local/bin/ruby我已经安装了ruby1.9.3p286、Rails3.2.8和rvm1.16.17(stable)我已经按照以下提示安装了Ruby和Rails:http://hivelogic.com/articles/compiling-ruby-rubygems-and-rails-on-snow-leopard接下来我想运行geminstallpg但出现错误:Nosu

ruby - Stripe 连接 : Charging an existing customer against a "connected" (Standalone) account

如果试图通过connectedaccount向客户记录(具有关联的信用卡)收费,我得到一个错误声明,“没有这样的客户:cus_xxxx”——尽管当不使用“已连接”帐户(通过平台账号)。例如,考虑以下Ruby代码,假设我们有一个ID为acct_ABC123的“已连接”(独立)帐户:#Usethe(secret)APIkeyforthe"platform"orbaseaccount.Stripe.api_key='sk_[...]'customer=Stripe::Customer.create(email:'customer@example.com')#Associateacredit-

ruby-on-rails - 炼油厂 CMS : generating migrations for existing models within an engine

我想向我在RefineryCMS引擎中创建的模型添加一个属性。我知道我可以执行以下操作:rails生成迁移AddPartNumberToProductspart_number:string手动将迁移文件从db/migrate移动到vendor/extensions/products/db/migrate但是是否有命令首先将迁移生成到正确的文件夹中?谢谢! 最佳答案 根据@parndt(theleaddeveloperoftheRefineryproject),我上面概述的方法是正确的。

ruby-on-rails - 使用 add_foreign_key : 'column "user_id"referenced in foreign key constraint does not exist' 进行 Rails 迁移

(Rails是5.0.0版本,Ruby是2.3.0p0)我想在我的用户表和卡片表之间创建关联。我已将belongs_to:user添加到Cards模型,并将has_many:cards添加到Users模型,并创建了一个迁移:classAddUserIdToCard当我运行rakedb:migrate时,我收到错误:ActiveRecord::StatementInvalid:PG::UndefinedColumn:ERROR:column"user_id"referencedinforeignkeyconstraintdoesnotexist:ALTERTABLE"cards"ADDC

ruby-on-rails - rails 生成器 : generate files based on already existing rails files

我想制作一个生成器,根据应用程序中已有的文件(例如,View或Controller)创建文件(和目录等)。所以如果我们有这样设置的View-app-views-layouts-application.html.erb-users-index.html.erb-show.html.erb-etc...我想基于它们创建我能做的文件(只用ruby​​)directories=Dir.entries("#{Rails.root}/app/views")directories.eachdo|directory|unlessdirectory=="."ordirectory==".."files=