Wednesday, 28 August 2013

Rails find_or_create_by strange results

Rails find_or_create_by strange results

With an empty database :
file = Foo.find_or_create_by_pattern_and_job_id(pattern: @pattern, job_id:
job) do |bar|
bar.file_status = 'NEW'
bar.file_modification_date = ftime
bar.last_filename = fn
end
puts file.inspect
=> #<Foo blah blah blah>
This is the expected result (for me).
But
If I have in my database an item with pattern = @pattern and job_id = job
then :
file = Foo.find_or_create_by_pattern_and_job_id(pattern: @pattern, job_id:
job) do |bar|
bar.file_status = 'NEW'
bar.file_modification_date = ftime
bar.last_filename = fn
end
puts file.inspect
=> nil
Why do I get this empty array? Is it possible to get the same result with
a find and with a create (to get the object directly)?
I tried the request generated by active record in my pgsql client and it
worked. So it's not a request issue.
Thanks in advance.

No comments:

Post a Comment