d3bug3r d3bug3r - 7 months ago 19
Ruby Question

Rails showing failed conditional result

Here is my method in controller:

def destroy
logger.info "*******************INCOMING ATTACHMENT #{params[:id]}**********************"
@prod = ProductAttachment.find(params[:id])
@product = Product.find(@prod.product_id)
if params[:id] == @product.cover_id
logger.info "*******************ATTACHMENT MATCHED**********************"
@prod = ProductAttachment.where(:product_id => @prod.product_id)
logger.info "*******************ATTACHMENT #{@prod.count}**********************"
else
logger.info "*******************ATTACHMENT NOT MATCHED**********************"

end
#@product_attachment.destroy
respond_to do |format|
format.html { redirect_to product_attachments_url, notice: 'Product attachment was successfully destroyed.' }
format.json { head :no_content }
end
end


Even though my
params[:id]
is equal to
1156
and
@product.cover_id
return
1156
my conditional loop still print out
ATTACHMENT NOT MATCHED
. Thanks!!

Answer

params[:id] is a string and @product.cover_id is an integer, you may change them both to string or integer for comparing.

Comments