Tony
Tony

Reputation: 10208

Invoke a model method within a rake task

My question is very simple but I could not find a correct answer for it. I have a rake task which invokes a model method.

task :post do
    BufferPreference.post
end

It doesn't work and throws the error uninitialized constant BufferPreference

I tryied adding the following require: require 'buffer_preferences' but I get the error no such file to load -- buffer_preference

My model is defined as follows:

class BufferPreference < ActiveRecord::Base

in the file buffer_preference.rb

Upvotes: 2

Views: 1672

Answers (1)

thesis
thesis

Reputation: 2575

If you want to run controller action as a method from the controller, it is not good practice. Try to move your code, into a Model.

Try this:

task :post => :environment do
    BufferPreference.post
end

BufferPreference.post that means you should have BufferPreference model, with class method called post.

Upvotes: 3

Related Questions