草庐IT

get_attribute

全部标签

ruby-on-rails - rails : How to get has_many associations of a model

如何获取模型的has_many关联?例如,如果我有这个类:classA我想要这样的方法:A.get_has_many返回[B,C]这可能吗?谢谢! 最佳答案 您应该使用ActiveRecordreflections.然后你可以这样输入:A.reflect_on_all_associations.map{|assoc|assoc.name}这将返回你的数组[:B,:C] 关于ruby-on-rails-rails:Howtogethas_manyassociationsofamodel,我

ruby - 在 Ruby 中检测按键(非阻塞)w/o getc/gets

我有一个简单的任务需要等待文件系统上的某些更改(它本质上是一个原型(prototype)编译器)。所以我有一个简单的无限循环,在检查更改的文件后休眠5秒。loopdo#iffileschanged#processfiles#andputsresultsleep5end而不是Ctrl+C敬礼,我宁愿能够测试并查看是否按下了某个键,而不会阻塞循环。本质上,我只需要一种方法来判断是否有传入的按键,然后是一种获取它们直到遇到Q的方法,然后退出程序。我想要的是:defwait_for_Qkey_is_pressed&&get_ch=='Q'endloopdo#iffileschanged#pro

ruby-on-rails - accepts_nested_attributes_for的替代方法-也许是virtus

我对rails比较陌生,最终找到了正确的使用accepts_nested_attributes_for的方法。然而,网络上有一些严肃的资源说,使用accepts_nested_attributes_for通常是一种不好的做法(比如这个one)。要避免accepts_nested_attributes_for需要做哪些更改,以及将附加类文件放在哪个文件夹中(我想需要一个附加类)。我读到virtus适合这个。对吗?下面是一个仍然使用accepts_nested_attributes_for的非常基本的示例(查找完整的示例here):模型classPerson控制器classPeopleCo

ruby-on-rails - rails : Plus sign in GET-Request replaced by space

在Rails3(Ruby1.9.2)中我发送一个请求StartedGET"/controller/action?path=/41_+"但是参数列表是这样的:{"path"=>"/41_","controller"=>"controller","action"=>"action"}这里出了什么问题?-、*或.符号工作正常,只是+将被空格替换。 最佳答案 这是正常的URL编码,theplussignisashorthandforaspace:Withinthequerystring,theplussignisreservedasshor

ruby - Ruby 中 const_get 的混淆行为?

根据文档mod.const_get(sym)“返回mod中命名常量的值。”我也知道const_get默认情况下可能会查找接收者的继承链。所以以下工作:classA;HELLO=:hello;endclassB:hello我也知道Ruby中的类是Object的子类,因此您可以使用const_get来查找“全局”常量,即使接收方是一个普通类:classC;endC.const_get(:Array)#=>Array然而,这就是我感到困惑的地方——模块不继承Object。那么,为什么我仍然可以使用const_get从模块中查找“全局”常量?为什么以下方法有效?moduleM;endM.con

Ruby:动态生成attribute_accessor

我正在尝试从散列(使用嵌套散列)生成attr_reader,以便它自动镜像instance_variable创建。这是我目前所拥有的:data={:@datetime=>'2011-11-23',:@duration=>'90',:@class=>{:@price=>'£7',:@level=>'all'}}classEvent#attr_reader:datetime,:duration,:class,:price,:leveldefinit(data,recursion)data.eachdo|name,value|ifvalue.is_a?Hashinit(value,recur

ruby-on-rails - 为什么 RSpec 的方法 "get"、 "post"、 "put"、 "delete"在 gem(或 Rails 外部)的 Controller 规范中不起作用?

我不是Rails或Rspec的新手,但我是制作gems的新手。当我测试我的Controller时,REST方法“get”、“post”、“put”、“delete”给我一个未定义的方法错误。您将在下面找到代码,但如果您更喜欢在馅饼中查看它,clickhere.谢谢!这是我的spec_helper:$LOAD_PATH.unshift(File.dirname(__FILE__))$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..','lib'))require'rubygems'require'active_support'u

ruby-on-rails - 抽取 Assets :precompile gets killed when there is a console session open in production

在我托管在digitalocean上的生产服务器上,如果有帮助的话,Ubuntu12.04,我有RoR4和rake10.1.1。当我部署时,我运行rakeassets:precompile,我注意到一个奇怪的问题,如果我在执行此操作时打开了一个railsconsolesession,我得到以下输出~#rakeassets:precompile~#Killed主要是很烦人,但我希望解决这个问题的原因是在雇用新开发人员时,会有部署/控制台冲突噩梦。谢谢,布莱恩 最佳答案 您的预编译进程可能被终止,因为您的内存不足。您可以通过在另一个ss

ruby-on-rails - Rails : how to get the current host, 或用它构建 URL?

这个问题在这里已经有了答案:GettingtheHostnameorIPinRubyonRails(12个答案)关闭8年前。我需要使用主机构建具有不同端口的URL。例如,如果主机是example.com,我需要生成类似http://example.com:8080/的URL我需要它是便携的,所以当我在本地环境中时,它会显示http://localhost:8080/相反。有什么想法吗?

ruby-on-rails - RSpec 测试使用 GET 参数重定向到 URL

假设我有一个带有redirect_to_baz方法的FoosController。classFoosController我正在使用spec/controllers/foos_controller_spec.rb对此进行测试:require'spec_helper'describeFoosController,:type=>:controllerdodescribe"GETredirect_to_baz"doit"redirectstoexample.comwithparams"doget:redirect_to_bazexpect(response).toredirect_to"htt