if params[:parent_type] == "Order"
parent_id = nil
else
parent_id = params[:parent_id]
end
Would a Ruby person laugh at me for writing it this way? It doesn't seem particularly concise like some Ruby code I've seen.
if params[:parent_type] == "Order"
parent_id = nil
else
parent_id = params[:parent_id]
end
Would a Ruby person laugh at me for writing it this way? It doesn't seem particularly concise like some Ruby code I've seen.
Nothing really wrong with it as-is, but can be made more concise:
parent_id = (params[:parent_type] == "Order") ? nil : params[:parent_id]
Alternatively:
parent_id = if (params[:parent_type] == "Order")
nil
else
params[:parent_id]
end
I think it's fine the way it is. I'm a Ruby person, and I wouldn't laugh at you for writing it that way. It's clear what the code does and there's no real code duplication, so I wouldn't worry about it.
That looks perfectly reasonable to me. You could move the assignment in front of the if ( parent_id = if params...
) or use the ternary, but I don't think the result would look better.
If parent_id is nil or undefined before that line you can simply write:
parent_id = params[:parent_id] unless params[:parent_type] == "Order"
I like:
parent_id = (params[:parent_type] == "Order" ? nil : params[:parent_id])
One more variation:
parent_id = (params[:parent_type] == "Order") && params[:parent_id]