Ruby on rails Rails 4-检查所有属性是否为真的方法

Ruby on rails Rails 4-检查所有属性是否为真的方法,ruby-on-rails,methods,Ruby On Rails,Methods,我正在尝试在rails 4中制作一个应用程序 我想在模型中编写一个方法来检查所有属性是否都为真 我正在努力: def ready_to_go if [ payment == true, && terms == true && identification == true && key_org == true && key_business == true &&

我正在尝试在rails 4中制作一个应用程序

我想在模型中编写一个方法来检查所有属性是否都为真

我正在努力:

def ready_to_go
    if 
    [ payment == true, 
    && terms == true
    && identification == true
    && key_org == true
    && key_business == true
    && docs == true
    && funding == true
    && contract == true
    && governance == true
    && internal == true
    && preferences == true
    && address == true
    && interest == true ]
    end
  end

有人知道这有什么问题吗?

这个
[…]
是错误的。它定义了一个数组。带有一个元素“false”的arry被解释为true。只需去掉括号(如果你真的不想弄糊涂的话,可以使用圆括号)

如果在同一行启动
。
如果只想返回true/false,则可以将
If
一起删除。 如果您的值是
true
false
nil
,您也不需要检查true:

def ready_to_go
  payment &&
  terms &&
  identification &&
  ...
end

定义方法并检查其是否准备就绪的最简单方法是:

def ready_to_go
  [ payment, terms, identification, key_org, key_business, docs, funding, contract, governance, internal, preferences, address, interest].all?
end

尝试
Array.all?

[true, false].all? # false  
[true, true].all? # true

首先,使用
符号将方法定义为谓词。然后删除与true的比较,只需使用
付款和条款&&&…

def ready_to_go?
  payment &&
  terms &&
  ...
end

定义错误,你的意思是它能更简洁吗?或者它不起作用了?我不明白为什么您有数组语法
[
]
,或者为什么它在
if
中没有任何内容。如果只想返回true或false,则删除If和数组syntaxinstead,然后在model
self.attributes
中这样尝试获取该模型的所有属性。