I am pretty inexperienced with MySQL queries, but I am currently learning them. How would I find all the nil fields of an object and then populate just one of those fields?
For example, I have an inventory - but I want to find all the empty slots to show me how many slots I have left and then find ones that are empty, and then to populate just one of those fields with a string or number.
How would I do this?
This is the code I attempted:
@cast = Cart.where("user_id = ? AND cart_slot_one = nil AND cart_slot_two = nil AND cart_slot_three = nil AND cart_slot_four = ? AND cart_slot_five = ? AND cart_slot_six = ? AND cart_slot_seven = ? AND cart_slot_eight = ? AND cart_slot_nine = ? AND cart_slot_ten = ?",
current_user.id, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil ).first
wherewhich has a SQL string supplied as an argument. This contrasts with the hash-argument version you can see in @akz92 answer. When you're using the SQL string version, you need to useIS NULLinstead of= nil. Regardless of this syntax issue, you should read about associations so you don't have to make named columns for each "cart slot".