On the other forum on stackoverflow, someone provided this as a
solution for the following use case: "I want to check by an unique ID
whether a record is already created. If it is, I want to add 1 to the
amount attribute. If it is not, I want to create it and set the amount
attribute to 1."
Here was their solution:
@have = current_user.haves.find_or_initialize_by_id(params[:have]) do |
h.amount = 0
I don't think it's a great one, but I have a question about the block.
Let's say the record already exists and so the find is triggered and
when the record is queried, it has an amount value of 3. When that
block is executed, it looks like the amount value would be assigned a
value of 0. Or am I missing something?