在交互式会话中工作,但在Rails系统测试中失败:
使用byebug,它会在show view中意外地返回空。
@item.children.where(done: false)如果我跳过ActiveRelation,如下所示,我可以正确地看到对象
@item.children
#<ActiveRecord::Associations::CollectionProxy [#<Item id: 980190964, title: "my item", done: false此命令意外地返回1条记录
@item.children.where.not(done:false)设置(工作正常)
setup do
@user = users(:one)
visit login_url
fill_in "Email", with: @user.email
fill_in "Password", with: 'secret'
click_on "Login"
end测试(工作正常,但项目不会显示!)
test "create one entry" do
visit item_url(@user.top_item)
fill_in('item[title]', with: 'my item')
find_field('item[title]').native.send_key(:enter)
visit current_path
assert_selector "h5", text: "parentObject", match: :one
endshow action的一部分:
@children = @item.children
@newItem = current_user.items.new显示视图的一部分:
<% @children.where(done: false).each do |item| %>甚至@children属性也是空的。重新加载不会产生任何影响。
发布于 2019-05-08 04:38:55
当我将下面的设置设置为false时,它起作用了。我直接在sqlite3中查看了模式,“done”字段的默认值是“false”而不是“0”。但为什么开发如此顺利,而不是测试呢?可能与仍在使用'false‘的yaml文件有冲突。因此,我将坚持使用不推荐使用的设置一段时间,直到我完全理解其含义,并可以稍后进行适当的迁移。
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = false我尝试true的原因:
不推荐使用警告:不推荐将ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer设置为false。SQLite数据库已使用't‘和'f’序列化布尔值,并且在将此标志设置为true之前,必须将旧数据转换为1和0(其本机布尔序列化)。转换可以通过设置rake任务来完成,该任务运行
ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)对于所有模型和所有布尔列,在此之后,必须通过将以下内容添加到application.rb文件将标志设置为true:
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = truehttps://stackoverflow.com/questions/56010236
复制相似问题