[katello-devel] find_or_create_by_#{attr} not a good idea

Lukas Zapletal lzap at redhat.com
Tue May 24 09:23:41 UTC 2011


On 05/24/2011 12:39 AM, Partha Aji wrote:
> Problem here is that the error is not immediately reported even if the validations in the model failed.

The race condition is not a problem for us. The code is safe since we do 
have unique index on the username column. If there is a race condition 
(cannot be in our seeds.rb script) an error will be reported through 
errors array because of index constraint. And the returned object will 
be nil. But I understand your motivation - no explicit check for nil.

But even when we change to find and create! errors wont be reported 
because of different thing in glue. Some validation errors are rescued, 
but only put in the errors array (not rethrown) even when exclamation 
mark method is used. I guess Dmitri should be working on it this sprint. 
At the end of the day glue layer should raise error when method with 
exclamation mark is used. Thus we should fix the latter prior to 
refactoring this.

The find_by_xy methods do have exclamation mark versions. It would be 
nice to have it also for find_or_create, that's for sure. I don't know 
if it worths to hack it.

-- 
Later,

  Lukas Zapletal | E32E400A
  RHN Satellite Engineering
  Red Hat Czech s.r.o. Brno




More information about the katello-devel mailing list