[katello-devel] find_or_create_by_#{attr} not a good idea
Partha Aji
paji at redhat.com
Mon May 23 22:39:36 UTC 2011
In the seeds.rb and many other places in the code we have code that looks like
user_admin = User.find_or_create_by_username(:username => 'admin', :password => 'admin')
Problem here is that the error is not immediately reported even if the validations in the model failed.
>From here http://blog.grayproductions.net/articles/five_activerecord_tips the preferred way seems to be separately doing a find and then doing a create! with the exclamation, to fail in the case of validation failures. Wonder if there is way to alias find_or_create_by_#{..} and have it automatically yell if validations fail..
Partha
More information about the katello-devel
mailing list