Ruby 无法检查Rails 5是否存在布尔值
所以我对if语句有点问题,我的数据库结构是这样的 所以我想检查布尔值是真是假,这就是我现在要做的Ruby 无法检查Rails 5是否存在布尔值,ruby,ruby-on-rails-5,Ruby,Ruby On Rails 5,所以我对if语句有点问题,我的数据库结构是这样的 所以我想检查布尔值是真是假,这就是我现在要做的 <% if @course_modules_users.where(complete: true).nil? %> <i class="fas fa-check text-green float-left mr-1"></i> <span class="text-xs mr-2">Completed</span> <% els
<% if @course_modules_users.where(complete: true).nil? %>
<i class="fas fa-check text-green float-left mr-1"></i>
<span class="text-xs mr-2">Completed</span>
<% else %>
<%= link_to complete_course_module_path(course_module.id), method: :put do %>
<i class="fas fa-check text-grey-darkest float-left mr-2"></i>
<% end %>
<% end %>
CoursesController显示方法
def show
course = Course.friendly.find(params[:id])
@course_modules = course.course_modules.order(created_at: :asc)
@course_modules_users = CourseModulesUser.all
end
希望这能有所帮助
编辑3
展示
@course\u modules\u users.wherecomplete:true将返回一个数组
[]和nil是不同的概念。[] != 无[]零?==错
在ruby中,除nil和false之外的所有内容都被认为是真实的。例如,这意味着[]是一个真实的值
将这些知识放在一起可以解释您在上面看到的一切:
<% if @course_modules_users.where(complete: true).nil? %>
这总是会发生的,因为任何数组,即使是空数组,在ruby中都是真实的
您可以通过多种方式编写此文档,例如:
<% if @course_modules_users.where(complete: true).empty? %>
或:
什么是@course\u modules\u users?@Pavan我在这里显示了course\u模块,但是course\u modules\u用户要跟踪模块和完成模块的用户。我有课程模块和课程模块用户,我是说你是如何定义@course\u modules\u users的?用代码更新问题SEE edit 2@Pavan希望这就是你想要的SEE edit 3@Pavan我明白了,它返回ActiveRecord::Collection。但其他的都是真的。是的,严格地说。我稍微简化了一下。@TomLord我试过你提供的例子,但我仍然看到@B.J.B哦。。。现在,您已经添加了@course\u modules\u users=CourseModulesUser.all的信息。当然应该是@course\u modules\u users=CourseModuleUser。而course\u module:@course\u modules?您只想检查这些模块的用户是否完整;然而,您当前的代码是检查任何模块的用户是否完整。
<% if @course_modules_users.where(complete: true).nil? %>
<% if @course_modules_users.where(complete: true) %>
<% if @course_modules_users.where(complete: true).empty? %>
<% if @course_modules_users.where(complete: true).none? %>