【问题标题】:Ruby how to get array with boolean?Ruby如何用布尔值获取数组?
【发布时间】:2021-06-10 03:45:35
【问题描述】:

我想获得一组船,但我希望只显示将租金布尔值设置为 true 的船。

如何做到这一点?

我已经添加:

@boats = Boat.where(rent: true)

但是没用

我什至尝试了方法模型

def rents
    self.class.where(rent: true)
  end

也没有用

【问题讨论】:

  • 你想要一个名字数组吗?或者你想要一个对象的散列?如果您为一组对象正确设置了表,@boats = Boat.where(rent: true) 应该可以工作。
  • 你说的没有用是什么意思?有错误吗?错误消息是什么?堆栈跟踪是什么样的?还是您收到了意想不到的结果?看起来怎么样?
  • 您的代码看起来没问题,当您尝试执行代码时是否出现错误?这可能会有所帮助。
  • @boats = Boat.where(rent: true) 应该可以工作。确保您确实拥有租用 = true 的船

标签: ruby-on-rails ruby sinatra


【解决方案1】:

你可以这样做

<% @boats.where(:rent=>true).each do |t| %> 
 <%= t.name %>
<%end%>
<%= @boats.where(:rent=>true).all %>

<% if boats.where(:rent=>true).exists?  %>
 <%= t.name %>
<%end%>

def rents
 @boats.where(:rent=>true).all
end

【讨论】:

  • 努尔萨!视图上的数组工作正常,但在控制器上却不行!非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 2015-11-12
  • 2014-05-28
  • 2016-08-13
相关资源
最近更新 更多