Back to top

    ReQL command: branch

    Command syntax

    r.branch(test, true_action[, test2, test2_action, ...], false_action) → any

    test.branch(true_action[, test2, test2_action, ...], false_action) → any


    Perform a branching conditional equivalent to if-then-else.

    The branch command takes 2n+1 arguments: pairs of conditional expressions and commands to be executed if the conditionals return any value but false or nil (i.e., “truthy” values), with a final “else” command to be evaluated if all of the conditionals are false or nil.

    You may call branch infix style on the first test. (See the second example for an illustration.)

    r.branch(test1, val1, test2, val2, elseval)

    is the equivalent of the Ruby statement

    if test1
    elsif test2

    Example: Test the value of x.

    x = 10
    r.branch((x > 5), 'big', 'small').run(conn)
    > "big"

    Example: As above, infix-style.

    x = 10
    r.expr(x > 5).branch('big', 'small').run(conn)
    > "big"

    Example: Categorize heroes by victory counts.

            r.row['victories'] > 100,
            r.row['name'].add(' is a superhero'),
            r.row['victories'] > 10,
            r.row['name'].add(' is a hero'),
            r.row['name'].add(' is very nice')

    If the documents in the table marvel are:

        { :name => "Iron Man", :victories => 214 },
        { :name => "Jubilee", :victories => 49 },
        { :name => "Slava", :victories => 5 }

    The results will be:

        "Iron Man is a superhero",
        "Jubilee is a hero",
        "Slava is very nice"

    Get more help

    Couldn't find what you were looking for?