0

I have the following task to run on Rais:

require 'net/http'
namespace :seed do

task :set_gid_on_histories => :environment do

    History.all.each do |hist|

        if hist.channel != "REMOTE"
            if hist.guest
                guest = Guest.find(hist.user_group_id)
                hist.update_attributes(:group_id => guest.group_id)    
            else
                ug = UserGroup.with_deleted.find(hist.user_group_id)
                hist.update_attributes(:group_id => ug.group_id)    
            end     

        end

    end
end

end

The problem happens in line

ug = UserGroup.with_deleted.find(hist.user_group_id)

What happens is that it's throwing

ActiveRecord::RecordNotFound: Couldn't find UserGroup with 'id'=71 [WHERE "user_groups"."deleted_at" IS NULL]

just as if there was no 'with deleted' on the command line.

I tried to type the same command on rails console and it worked, i.e. when I run ug = UserGroup.with_deleted.find(71) it works just fine and finds the deleted user.

So it seems that paranoia's 'with_deleted' command is not working on rails tasks... Am I missing something?

Casanova
  • 33
  • 6
  • Which version of rails are you using? You should be able to get around it by using `UserGroup.unscoped.find(hist.user_group_id)` – j-dexx Apr 18 '16 at 15:20
  • I'm using Rails 4.1.8. I tried unscoped as you said, but it was exactly the same: it works on rails console, but not on the task – Casanova Apr 18 '16 at 16:54
  • I have a clue: I'm running the task with 'rvmsudo RAILS_ENV=production rake seed:set_gid_on_histories'. If I take out RAILS_ENV=production from it, it understands the with_deleted/unscoped commands (it worked on my local database) – Casanova Apr 26 '16 at 12:53
  • But the problem persists because on my server I must use RAILS_ENV=production because my db is there. So the problem persists but its related to the Rails Environment – Casanova Apr 26 '16 at 12:55
  • It's weird to me it would work like that only in one environment. I think you'll need to file an issue with the gem's maintainer on github, including as much info as you can – j-dexx Apr 26 '16 at 13:40

0 Answers0