I have a few validations for my votership has_many :through model. However, i’m not quite sure how to handle validation errors since i’m using create! Here’s my code:
def cast_vote_up!(user_id, direction)
voterships.create!(:issue_id => self.id, :user_id => user_id,
:direction => direction)
end
this is within my Issue model. I have issues and users and users vote on issues via the join model. How do I handle errors in validations since I am just using create! Usually I would do something like
if @model.save
#code
else
#other code
end
but without the .save i’m not sure what to do
create!will throw an error on failure, but if you usecreate(without the!) you can handle the error the same way you do withsaveUsing the bang (
!) basically means you’re asking your app to cease further execution immediately, if something goes wrong. There are situations where that might be desirable, but when dealing with forms, and user input, you’ll need to use the non-bang version because people often make simple typing mistakes that should be caught and dealt with, rather than showing an application error page.