我的问题的简短版本
在Rails ActiveRecord中,如果我有一个布尔字段并且我给它分配了类似abc
"或"的东西2
,那么它会立即转换为false
.该值1
将转换为true
,并nil
保持为nil
.为什么会这样?我在哪里可以找到解释此行为的Rails文档(或Ruby文档)?
我的问题的长版本
我很难理解Rails如何处理Boolean
为Rails模型中的字段赋值的尝试.例如,假设我有一个Website
模型,其中包含一个String
被调用:domain
的Boolean
字段和一个被调用的字段:can_ssl
.
我的迁移看起来像这样:
class CreateWebsites < ActiveRecord::Migration
def change
create_table :websites do |t|
t.string :domain
t.boolean :can_ssl, :default => false
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
在我的模型文件中,我添加了一些验证规则,所以它看起来像这样:
class Website < ActiveRecord::Base
validates :domain, :presence => true
validates :can_ssl, :inclusion => { :in => [true, false] }
end
Run Code Online (Sandbox Code Playgroud)
很简单.基于我做了什么,我期待那:can_ssl
只能设置为值true …
have_selector
在尝试匹配文本完全匹配的元素时,我很难使用Capybara .我知道这可以使用正则表达式完成,但是我阅读Capybara的博客文章让我相信我可以使用:exact
arg或set Capybara.exact = true
.我目前正在使用Capybara 2.2.1.这是我有的:
假设我有一个页面(称为"测试页面").在这个页面是这样的:
<div id="my_div">abcdef</div>
Run Code Online (Sandbox Code Playgroud)
我有一个Cucumber功能测试,看起来像这样:
Feature: Test for exact text matches in have_selector
Scenario:
Given I am on the test page
Then I should get a partial text match "bcde" in my div
And I should get a partial text match "abcdef" in my div
And I should get an exact text match using regexp "abcdef" in my div
And I should not get an exact text …
Run Code Online (Sandbox Code Playgroud)