jjzjj

boolean_var

全部标签

ruby - 怎么来的(a_method || :other) returns :other only when assigning to a var called a_method?

给定以下方法:defsome_method:valueend以下语句按我的预期工作:some_method||:other#=>:valuex=some_method||:other#=>:value但是下面语句的行为让我感到困惑:some_method=some_method||:other#=>:other它按预期创建了一个名为some_method的局部变量,随后对some_method的调用返回该局部变量的值。但为什么它分配:other而不是:value呢?我知道这可能不是一件明智的事情,并且可以看出它可能有多么模棱两可,但我认为应该在考虑作业之前评估作业的右侧...我已经在R

ruby-on-rails - 在 Ruby on Rails 中添加 boolean 列值

我正在开发一个创建网络博客的RubyonRails项目。我希望将一个名为featured的boolean数据库字段添加到Post模型中。该字段应该可以通过我添加的事件管理界面进行编辑。我使用了以下代码,但我什至没有在网站上显示另一列。$railsgeneratemigrationaddFeaturedfeatured:boolean$rakedb:migrate我是RubyonRails的新手,非常感谢任何帮助。我的index.html.erb文件中的相关代码(views):FeaturedPost架构.rb:ActiveRecord::Schema.define(:version=>

ruby-on-rails - connect() 到 unix :/var/run/unicorn. 连接到上游时 sock 失败(111:连接被拒绝)

我遵循ruby​​onrails一个应用程序点击部署。数据库做得很好,即使我检查Rails控制台一切正常017/02/2615:34:17[error]18564#0:*31connect()tounix:/var/run/unicorn.sockfailed(111:Connectionrefused)whileconnectingtoupstream,client:121.52.156.57,server:_,request:"GET/HTTP/1.1",upstream:"http://unix:/var/run/unicorn.sock:/",host:"188.166.157

Ruby 按 boolean 值和数字排序

我正在使用Ruby1.8.7。我有以下哈希数组。我需要先按boolean值排序,但这些结果也必须按原始顺序排序。我基本上需要将所有真正的哈希转移到数组的顶部,但保持原始顺序。如有任何帮助,我们将不胜感激!array=[{:id=>1,:accepts=>false},{:id=>2,:accepts=>false},{:id=>3,:accepts=>true},{:id=>4,:accepts=>false},{:id=>5,:accepts=>true}]sorted=array.sortdo|x,y|ifx[:accepts]==y[:accepts]0elsifx[:accep

ruby-on-rails - 对于 Rails 中的某些情况,如何实现是/否而不是 boolean 值?

我是编程新手,但在我的应用程序中我希望某些情况显示"is"或“否”而不是“真”或“假”。我不确定最好的方法,我读过this问题,但并没有真正理解如何实现它。有人能帮我吗,最好把它放在初始化程序、帮助程序或其他地方吗?我希望能够在我希望显示是/否的任何地方调用我的View中的内容,或者创建一个自定义数据类型,在我的迁移中我可以创建类似t.boolean_yesno的内容,然后为我做的每一列它只会将true存储为yes,将false存储为no。如果有人帮助我走上正轨,我将不胜感激,我没有使用初始化器或助手的经验。谢谢! 最佳答案 语言环

ruby-on-rails - 从 Virtus.model 动态扩展时使用 boolean 属性辅助方法

假设我有一个带有boolean属性active的Virtus模型User:classUserincludeVirtus.modelattribute:active,Boolean,default:false,lazy:trueend然后我可以使用辅助方法active?:User.new.active?#=>falseUser.new(active:true).active?#=>true但是当我尝试从Virtus.model中扩展并动态定义一个boolean属性时:classUser;enduser=User.newuser.extend(Virtus.model)user.attri

ruby - `var = something rescue nil` 行为

在Ruby中,您可以在赋值结束时编写rescue以捕获可能出现的任何错误。我有一个函数(如下:a_function_that_may_fail),如果不满足某些条件,让它抛出错误很方便。以下代码运行良好post={}#OtherHashstuffpost['Caption']=a_function_that_may_failrescuenil但是,如果函数失败,我什至不希望设置post['Caption']。我知道我能做到:beginpost['Caption']=a_function_that_may_failrescueend但这感觉有点过分-有更简单的解决方案吗?

ruby-on-rails - 无法连接到服务器 : "/var/run/postgresql/.s.PGSQL.5432"?

我在RubyOnRails中有一个带有postgresql数据库的简单网页,但是当我运行服务器时出现此错误,我不知道我这样做了。我使用postgresql,因为heroku需要应用程序在postgresql中。我在ubuntu13.10上工作错误是:PG::ConnectionBad无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受Unix域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?我需要帮助谢谢 最佳答案 像这样创建一个软链接(softlink),这对我有用$sudoln-

ruby-on-rails - Elasticsearch /轮胎 : How do I filter a boolean attribute?

我想过滤我类(class)的私有(private)bool值,以便它只显示非私有(private)但对我不起作用的资源。(我大大简化了代码)mappingdoindexes:private,type:"boolean"indexes:name,type:"string"endenddefself.search(params)tire.search(load:true,page:params[:page],per_page:20)doquery{stringparams[:query]}ifparams[:query].present?#SofarI'vetried...#filter:

ruby-on-rails - postgresql 数据库错误 : Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

当我运行rakedb:migrate或运行railss命令时,我得到同样的错误:Error:couldnotconnecttoserver:NosuchfileordirectoryIstheserverrunninglocallyandacceptingconnectionsonUnixdomainsocket"/var/run/postgresql/.s.PGSQL.5432"?当我尝试railss时,浏览器出现错误。这是我的database.ymldefault:&defaultadapter:postgresqlencoding:unicodepool:5development