I'm new to RoR and I'm now confusing about updating data to databases. And I studied RoR just recently, if the question is unclear please let me know.
Assume I created a table "book", with 3 columns: "name", "price", and "author".
When implementing action update in rails, I'll do the following:
def update
@book = Book.find(params[:id])
if @book.update({'name': 'Ruby tutorial'})
redirect_to @book
else
render 'edit'
end
end
This will update the record in database by
if @article.update({'name': 'Ruby tutorial'})
In order to test some failure cases, I modified the database column "name" to "nane" on purpose, I thought the statement if @article.update will fail due to the wrong table field name and the code will go to else block. Because I thought the statement is for checking whether rails saves the record into the database successfully.
However, my code throws exception because of the wrong field name instead of going to the else block.
I got confused about this behavior, what kinds of situation will cause if @article.update(params) fail and go to the else block?
Is if @article.update(params) for just validating form data? i.e, checking whether the hash key sent from the form is correct, as for database side(field name or database connection error) , is not the business of this statement so it will throw exception.
Thanks a lot.