草庐IT

guide-flashing-with-heimdall.878686

全部标签

ruby-on-rails - rails : comparison of Status with Status failed

我需要获取所有current_user.friends状态,然后按created_at对它们进行排序。classUser在Controller中:defindex@statuses=[]current_user.friends.map{|friend|friend.statuses.each{|status|@statusesa.created_at}endcurrent_user.friends返回对象数组Userfriend.statuses返回对象数组Status错误:comparisonofStatuswithStatusfailedapp/controllers/welcom

ruby-on-rails - rails 4 : how to use named scope with has_many associations

在我的Rails4应用程序项目(模型)has_many视频(模型)中。我在视频模型中有一个命名范围:scope:live,where(is_deleted:0,sent_to_api:1)在我的一个项目View中,我这样做(项目是项目的一个实例):project.videos.live.size我希望得到的是那个特定项目中的项目数量,但我得到的是任何项目中的视频数量。就好像.live不是从.videos返回一个子集而是替换它。我看到它解释了here将命名范围相互链接起来应该与逻辑AND相结合,但是当应用于“关联方法”时[.videos在这种情况下的正确术语]似乎并不即将发生。正确的做法

ruby-on-rails - has_many :through with counter_cache

据我了解,在定义:counter_cache选项时,应在包含belongs_to声明的模型上指定它。所以我有点不确定在通过关联使用has_may时如何处理这个问题(因为我相信在这种情况下不使用belongs_to声明):classPhysician:appointmentsendclassAppointmentappointment_countendclassPatient我希望使用:counter_cache选项来更有效地查找属于医生的患者数量。myPhysician.patients.count仅供引用:Rails3.1干杯 最佳答案

ruby 1.9 : Convert byte array to string with multibyte UTF-8 characters

我正试图在Ruby中找到一种方法来获取UTF-8字节数组并将其转换回字符串。在irb(Ruby1.9.2预览版3)中,我可以从UTF-8字符串创建正确的字节数组:ruby-1.9.2-preview3>'Café'.bytes.to_a=>[67,97,102,195,169]但是,我找不到从字节返回数组的方法。我尝试将Array.pack与U*选项一起使用,但这不适用于多字节字符。ruby-1.9.2-preview3>[67,97,102,195,169].pack('U*')=>"Café"有没有人知道如何将包含多字节字符的UTF-8字节数组转换回字符串?谢谢。

ruby-on-rails - rails 4 : How to use includes() with where() to retrieve associated objects

我不知道如何使用.where()检索关联模型数据的方法。在此示例中,项目belongs_to用户...classProjectparams[:id]}).firstendend在App/views/projects/invite.html.erg返回:---!ruby/object:Projectattributes:id:22name:SomeProjectNamebelongs_to:1instructions:Blablablaactive:truemax_duration:2max_videos:created_at:2013-08-2615:56:50.000000000Zu

ruby-on-rails - 如何测试依赖: :destroy with RSpec?

我想测试我的User模型关联has_many:projects,dependent::destroy现在已经走了这么远:it"destroysdependentprojects"douser=FactoryGirl.build(:user)project=FactoryGirl.build(:project)user.projects但这给出了一个错误:Failure/Error:expect(Project.count).tochange(-1)ArgumentError:`change`requireseitheranobjectandmessage(`change(obj,:ms

ruby-on-rails - ruby 正则表达式错误 : incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

我遇到了两个错误,都与编码有关并且都相关。我在启动WEBrick时遇到的第一个错误(技术上是警告):/Users/USERNAME/example/config/initializers/bb-ruby.rb:54:warning:invalidUnicodeProperty\P:/\:\-?\P/它所指的行是:/\:\-?\P/,这只是一些正则表达式,最终是这个block的一部分:@@tags['Razzing']=[/\:\-?\P/,'','Razzing',':P',:razzing]然后,我在解析一些字符串时也得到了以下错误(大概是由于同一行)...Encoding::Com

ruby - each_with_index_do 索引从 1 开始

我在Rails应用程序的View上使用ruby​​迭代器,如下所示:...我想加上1..而不仅仅是说:@document.data会得到让上面的索引从1开始的技巧。但是,遗憾的是,上面的代码索引仍然是0到data.length(实际上是-1)。那么我做错了什么,我需要索引等于1-data.length...不知道如何设置迭代器来执行此操作。 最佳答案 除非你使用像1.8这样的旧Ruby(我认为这是在1.9中添加的,但我不确定),你可以使用each.with_index(1)来获得1-基于枚举器:在你的情况下它会是这样的:...

关于Document mapping type name can‘t start with ‘_‘, found: [_update]

在修改elasticsearch时,用_update进行局部修改,修改失败,报错{    "error": {        "root_cause": [            {                "type": "invalid_type_name_exception",                "reason": "Document mapping type name can't start with '_', found: [_update]"            }        ],        "type": "invalid_type_name_exce

ruby-on-rails - rails 4 : Flash message persists for the next page view

我在布局中使用以下代码来显示两种类型的即显消息:它们都工作正常,但无论何时触发一个,它仍会出现一次额外的页面View。我没有使用任何缓存gem。为什么会这样?我该如何解决? 最佳答案 使用flash.now而不是flash.flash变量旨在在redirect之前使用,并且它会在一个请求的结果页面上持续存在。这意味着如果我们不redirect,而不是简单的render一个页面,flash消息将持续存在两个请求:它出现在呈现的页面上但仍在等待重定向(即第二个请求),因此如果您单击链接,消息将再次出现。为了避免这种奇怪的行为,在渲染而不