We can use the ternary operator Ruby. Something like that:
parent_id = params[:parent_type] == "Order" ? nil : params[:parent_id]
If parent_id is set with nil value, we can write if syntax:
parent_id = params[:parent_id] unless params[:parent_type] == "Order"
Or write simple Ruby if-else differently, as you did:
parent_id = if (params[:parent_type] == "Order") nil else params[:parent_id] end
For a more Idiomatic Ruby coding method, I found an amazing article about this.
source share