我的班级有以下范围Collection:
scope :with_missing_coins, joins(:coins).where("coins.is_missing = ?", true)
Run Code Online (Sandbox Code Playgroud)
我可以跑步Collection.with_missing_coins.count并得到一个结果 - 它很棒!目前,如果我想获得不丢失硬币的收藏品,我会添加另一个范围:
scope :without_missing_coins, joins(:coins).where("coins.is_missing = ?", false)
Run Code Online (Sandbox Code Playgroud)
我发现自己写了很多这些"相反"的范围.是否有可能在不牺牲可读性或求助于lambda /方法(需要true或false作为参数)的情况下获得范围的反面?
像这样的东西:
Collection.!with_missing_coins
Run Code Online (Sandbox Code Playgroud) 我正在运行Ruby on Rails 3.0.10和Ruby 1.9.2.我使用以下正则表达式来匹配名称:
NAME_REGEX = /^[\w\s'"\-_&@!?()\[\]-]*$/u
validates :name,
:presence => true,
:format => {
:with => NAME_REGEX,
:message => "format is invalid"
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我试图保存一些像下面这样的词:
Oilalà
Pì
Rùby
...
# In few words, those with accented characters
Run Code Online (Sandbox Code Playgroud)
我有一个验证错误"Name format is invalid..
我怎样才能改变上述正则表达式,从而匹配等也重音符号à,è,é,ì,ò,ù,...?
<% if dashboard_pane_counter.remainder(3) == 0 %>
do something
<% end>
Run Code Online (Sandbox Code Playgroud)
如果未定义dasboard_pane_counter,如何将其评估为false而不是抛出异常?
我知道我可以没有它,但这个问题一直困扰着我.
是否有一个Ruby语言相当于Groovy的Elvis运算符(?:)?
从本质上讲,我希望能够缩短这一点
PARAM = ARGV[0] ? ARGV[0] : 'default'
Run Code Online (Sandbox Code Playgroud)
或者等价
PARAM = 'default' unless PARAM = ARGV[0]
Run Code Online (Sandbox Code Playgroud)
变成这样的事情
PARAM = ARGV[0] ?: 'default'
Run Code Online (Sandbox Code Playgroud) 在两个(或更多)结果中的任何一个都可以接受的情况下编写rspec的最佳方法是什么?
这是我想要做的一个例子.这显然是错误的(我认为),但它应该给你我想要完成的事情的要点:
it "should be heads or tails" do
h="heads"
t="tails"
flip_coin.should be(h || t)
end
Run Code Online (Sandbox Code Playgroud)
是的,我知道我可以编写自己的rspec匹配器"should_be_one_or_the_other(option1,option2)",但这似乎有点多 - 我希望有更好的解决方案.
我想我已经尝试了所有关于这个问题的建议.仍然不适合我.
当我尝试捆绑...
$ bundle
...
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. …Run Code Online (Sandbox Code Playgroud) 下面的代码产生以下错误:OpenSSL :: SSL :: SSLError:SSL_connect SYSCALL返回= 5 errno = 0状态= SSLv3读取服务器问候A
require 'net/https'
uri = URI.parse("https://<server>.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.ssl_version = 'SSLv3'
http.get(uri.request_uri)
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?我尝试了所有其他问题中提到的一切,但仍然没有运气.
更新我
尝试以下方法:
更新II
仍然没有运气.
更新III
好的,这是最终的代码 - 感谢Steffen(见下面的答案):
require 'net/https'
uri = URI.parse("https://<server>.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.ssl_version = :TLSv1
http.ciphers = ['RC4-SHA']
http.get(uri.request_uri)
Run Code Online (Sandbox Code Playgroud)
我怀疑我的问题与其他人有关,因为它与远程配置错误的服务器有关.
我正在编写一个使用普通旧Ruby对象(PORO)从控制器中抽象出授权逻辑的应用程序.
目前,我有调用自定义异常类NotAuthorized,我rescue_from在控制器级别,但我很好奇,想知道:是否导轨4已经配备了一个例外,表明一个动作没有被授权?我是通过实施这个例外来重新发明轮子的吗?
澄清:raise AuthorizationException控制器内部没有发生任何事情,它发生在控制器外部完全分离的PORO内部.该对象不了解HTTP,路由或控制器.
有一个模块MyModule:
module MyModule
extend ActiveSupport::Concern
def first_method
end
def second_method
end
included do
second_class_method
end
module ClassMethods
def first_class_method
end
def second_class_method
end
end
end
Run Code Online (Sandbox Code Playgroud)
当某个类include是这个模块时,它将有2个方法暴露为实例方法(first_method和second_method)和2个类方法(first_class_method和second_class_method) - 很明显.
有人说,那
included块将在包含模块的类的上下文中执行.
这究竟是什么意思?意思是,什么时候second_class_method执行这个方法()?
我在迁移中有以下内容:
create_table :model_with_a_long_name do |t|
t.references :other_model_with_an_equally_long_name, index: true
end
Run Code Online (Sandbox Code Playgroud)
这会为Postgres产生一个名字太长的索引.
有没有办法手动指定索引名称(不分别添加整数列和索引)?
类似于以下内容:
create_table :model_with_a_long_name do |t|
t.references :other_model_with_an_equally_long_name, index: true, index_name: 'model_and_other'
end
Run Code Online (Sandbox Code Playgroud)
?
ruby ×10
bundle ×1
erb ×1
groovy ×1
named-scope ×1
net-http ×1
openssl ×1
postgresql ×1
regex ×1
rspec ×1