Reputation: 233
Can anybody can explain me this line of code
<% child_nodes = node.is_a?(Department) ? node.offices.where(parent_office_id: nil) : node.branch_offices %>
I am got confused by ? :
what this is called ? :
Upvotes: 0
Views: 52
Reputation: 915
It's called ternary operator, which has form condition ? if_true : if_false
You could write it using if
and else
.
<%
if node.is_a?(Department)
child_nodes = node.offices.where(parent_office_id: nil)
else
child_nodes = node.branch_offices
end
%>
Upvotes: 1
Reputation: 1230
It's a ternary operator. Another way to write this would be:
<%
if node.is_a?(Department)
child_nodes = node.offices.where(parent_office_id: nil)
else
child_nodes = node.branch_offices
end
%>
Upvotes: 0
Reputation: 76
So this statement contains a ternary operator: child_nodes = node.is_a?(Department) ?
If it evaluates to true the first condition is executed i.e.: node.offices.where(parent_office_id: nil)
Otherwise this code is ran: node.branch_offices
Hope this helps!
Upvotes: 1