草庐IT

find_element

全部标签

ruby-on-rails - 在 Rails 中,find_each 和 where 之间有什么区别?

在Rails中,find_each和where用于从ActiveRecord支持的数据库中检索数据。您可以将查询条件传递给where,例如:c=Category.where(:name=>'Ruby',:position=>1)并且您可以将批量大小传递给find_each,例如:Hedgehog.find_each(batch_size:50).map{|p|p.to_json}但是下面两段代码有什么区别呢?#code1Person.where("age>21").find_each(batch_size:50)do|person|#processingend#code2Person.

ruby 风格 : How to check whether a nested hash element exists

考虑存储在散列中的“人”。两个例子是:fred={:person=>{:name=>"Fred",:spouse=>"Wilma",:children=>{:child=>{:name=>"Pebbles"}}}}slate={:person=>{:name=>"Mr.Slate",:spouse=>"Mrs.Slate"}}如果“person”没有任何child,则“children”元素不存在。所以,对于Slate先生,我们可以检查他是否有parent:slate_has_children=!slate[:person][:children].nil?那么,如果我们不知道“slat

ruby-on-rails - rails ActiveRecord : Find All Users Except Current User

我觉得这应该很简单,但是我的脑子短路了。如果我有一个代表当前用户的对象,并且想查询除当前用户之外的所有用户,考虑到当前用户有时可能为nil,我该怎么做?这就是我现在正在做的:defindex@users=User.all@users.deletecurrent_userend我不喜欢的是我正在对查询结果进行后处理。除了感觉有点不对之外,如果我将查询转换为使用will_paginate运行,我认为这不会很好地工作。关于如何通过查询执行此操作的任何建议?谢谢。 最佳答案 可以在Rails4及更高版本中执行以下操作:User.where.

ruby - "Could not find a valid gem in any repository"(rubygame 等)

一段时间以来我一直在尝试安装这个“rubygame”gem,但是每当我使用命令时geminstallrubygame会报错:ERROR:Couldnotfindavalidgem'rubygame'(>=0)inanyrepositoryERROR:Whileexecutinggem...(Gem::RemoteFetcher::FetchError)Errno::ETIMEDOUT:Connectiontimedout-connect(2)(http://rubygems.org/latest_specs.4.8.gz)我也尝试过其他gems但结果相似:ERROR:Couldnotf

ruby-on-rails - rails : An elegant way to display a message when there are no elements in database

我意识到我正在编写很多与此类似的代码:Youhavenomessages.Ruby和/或Rails中是否有任何构造可以让我跳过它第一个条件?那么当迭代器/循环一次都不会进入时会执行吗?为了示例:Youhavenomessages. 最佳答案 你也可以这样写:Youhavenomessages. 关于ruby-on-rails-rails:Anelegantwaytodisplayamessagewhentherearenoelementsindatabase,我们在StackOverfl

ruby-on-rails - Rails has_many :through Find by Extra Attributes in Join Model

Ruby和Rails的新手,但我现在已经受过书本教育(这显然没有任何意义,哈哈)。我有两个模型,Event和User通过表EventUser连接classUser:event_usersendclassEventUser:event_usersend这个项目是一个日历,我必须在其中跟踪人们为给定的事件注册和刮掉他们的名字。我认为多对多是一种好方法,但我不能这样做:u=User.find:firstactive_events=u.events.find_by_active(true)因为事件实际上没有额外的数据,所以EventUser模型有。虽然我可以做到:u=User.find:fir

ruby-on-rails - 当没有记录时 find() 为 nil

在我当前的Rails程序中,当我使用类似的东西时user=User.find(10)当没有ID=10的用户时,我会有如下异常:ActiveRecord::RecordNotFound:Couldn'tfindUserwithID=10我可以得到nil而不是引发异常吗,所以当我做类似的事情时:unlessuser=Challenge.find(10)puts"someerrormsg"end我只想在没有记录的时候得到nil,我不想使用begin/rescue谢谢 最佳答案 是的,只是做:Challenge.find_by_id(10)

ruby-on-rails - ActiveRecord.find(array_of_ids),保留顺序

当您在Rails中执行Something.find(array_of_ids)时,结果数组的顺序不取决于array_of_ids的顺序。有什么办法可以找到并保留顺序吗?ATM我根据ID的顺序手动对记录进行排序,但这有点蹩脚。UPD:如果可以使用:order参数和某种SQL子句指定顺序,那么如何? 最佳答案 奇怪的是,没有人提出这样的建议:index=Something.find(array_of_ids).group_by(&:id)array_of_ids.map{|i|index[i].first}除了让SQL后端执行它之外,尽

ruby-on-rails - Rails:Gemfile.lock 需要 "Could not find bundler"(2.2.11)。 ( gem ::GemNotFoundException)

当我尝试执行bundlerupdate时出现此错误:.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in`to_specs':Couldnotfindbundler(>=0)amongst[rake-0.8.7,rake-0.8.7,rubygems-update-1.8.4](Gem::LoadError)我是Ruby的新手,有人能告诉我这是什么原因吗?安装了Rake0.8.7。 最佳答案 如果您安装了bundlerge

Ruby 数组 find_first 对象?

我是否遗漏了Array文档中的某些内容?我有一个数组,其中最多包含一个满足特定条件的对象。我想有效地找到那个对象。我从文档中得到的最好的想法是:candidates=my_array.select{|e|e.satisfies_condition?}found_it=candidates.firstif!candidates.empty?但我不满意有两个原因:select让我遍历了整个数组,尽管我们本可以在第一次命中后放弃。我需要一行代码(带有条件)来压扁候选人。这两种操作都是浪费的,因为预先知道有0或1个令人满意的对象。我想要的是这样的:array.find_first(block)