rails 4 scope NOTNULL重构

时间:2016-02-28 06:37:07

标签: ruby-on-rails postgresql refactoring ruby-style-guide

我必须创建一个用于创建活动作业的作用域,但这感觉有点古怪,它与PosgresSQL紧密耦合:

public static void main(String[] args) {
    if (args.length < 2 || !args[0].matches("\\d+[.\\d+]*")
            || !args[1].matches("\\d+[.\\d+]*")) {
        System.out.println("Plz pass 2 command line argument as numbers (0-9) only");
        return;
    }
    BigDecimal a = new BigDecimal(args[0]);
    BigDecimal b = new BigDecimal(args[1]);
    BigDecimal c = a.add(b);
    System.out.println("Sum is " + c);
}

你会这样写吗?谢谢

2 个答案:

答案 0 :(得分:2)

短&amp;更漂亮的版本将是这样的:

scope :active, -> { where.not(reviewed_at: nil, paid_at: nil).where('end_at >= ?', Date.today) }

答案 1 :(得分:0)

您可以在rails 4中使用where.not

scope :active, -> { where.not(reviewed_at: nil).where.not(paid_at: nil).where('end_at >= ?', Date.today) }