If I do
widget = Widget.find_or_create_by_widgetid(:widgetid => "12345", :param2 => "folk")
etc. then how do I tell if newobj is a found or newly created Widget? Is there something I can test conditionally on widget that will tell me?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t believe there’s a way to tell if the object is newly created or was already there. You could use
find_or_initialize_by_widgetidinstead which doesn’t save the new object. You can then checkwidget.new_record?which will tell you whether the object has been saved or not. You’d have to put a save call in the block of code for a new object but as you want to make that check anyway it shouldn’t ruin the flow of the code.So: