Sam
Sam

Reputation: 169

Implementing a schedular task using whenever gem in rails 3

I'm trying to implement a scheduler task that deletes a user in user table who got abused more than 5 times. To achieve this in the user.rb file I have return a method report_abuse_delete method which performs the functionality of finding the user who got abuses more than 5 times and delete his records from the database.

Here is my method in User model:

def report_abuse_delete

@delete_abused_user= Abuse.find(:all, :conditions=>['count>=?',5])

 @delete_abused_user.each do |d|

    @abused_user= User.find(d.abuse_id)

      if  @abused_user.delete

         render :text=> "User account has been deleted. Reason: This user has been reported spam for more than 5 times"

        UserMailer.user_delete_five_spam_report(@user).deliver

      end

 end
end

And this is what I have written in the Scheduler.rb file

every 2.minutes do
   rake "log:clear", :environment => "development"

   runner "User.report_abuse_delete", :environment => "development"
end

As you can see in the scheduler.rb file I'm trying to perform a 2 functions one is clearing my log for every 2minutes and trying to run a method report_abuse_delete that I wrote in my model.

I'm facing a issue as follows for every 2 minutes my log is getting cleared but the method which I wrote in the model in not getting invoked I guess the functionality is not getting triggered. I have searched all the web and checked every possible way. I'm unable to figure out what was the problem is.

Help me out please. Any kind of help is welcome and appreciable.

Upvotes: 0

Views: 526

Answers (1)

Chowlett
Chowlett

Reputation: 46657

You've defined report_abuse_delete as a normal - that is instance - method, but you're calling it as a class method. Try defining the method as def self.report_abuse_delete.

Also, I don't know if the render call will work: I haven't used this gem, but since you don't have any kind of user agent to see the text, I'm not sure what you'd expect it to do.

Upvotes: 1

Related Questions